kamangir commited on
Commit
56964c4
1 Parent(s): a856235

validating single image predict for fashion_mnist - kamangir/bolt#692

Browse files
image_classifier/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
  name = "image_classifier"
2
 
3
- version = "1.1.151"
4
 
5
  description = "fashion-mnist + hugging-face + awesome-bash-cli"
 
1
  name = "image_classifier"
2
 
3
+ version = "1.1.156"
4
 
5
  description = "fashion-mnist + hugging-face + awesome-bash-cli"
image_classifier/__main__.py CHANGED
@@ -1,4 +1,5 @@
1
  import argparse
 
2
  from . import *
3
  from .classes import *
4
  from .funcs import *
@@ -108,7 +109,7 @@ elif args.task == "predict":
108
  default=None,
109
  )
110
 
111
- success = classifier.predict(
112
  test_images / 255.0,
113
  test_labels,
114
  args.output_path,
@@ -132,10 +133,25 @@ elif args.task == "predict_image":
132
  success, image = file.load_image(image_filename)
133
 
134
  if success:
135
- success = classifier.predict(
 
 
 
 
 
 
 
 
 
136
  image / 255.0,
137
  output_path=args.output_path,
138
  )
 
 
 
 
 
 
139
  elif args.task == "preprocess":
140
  success = preprocess(
141
  args.output_path,
 
1
  import argparse
2
+ import cv2
3
  from . import *
4
  from .classes import *
5
  from .funcs import *
 
109
  default=None,
110
  )
111
 
112
+ success, prediction = classifier.predict(
113
  test_images / 255.0,
114
  test_labels,
115
  args.output_path,
 
133
  success, image = file.load_image(image_filename)
134
 
135
  if success:
136
+ image = cv2.resize(
137
+ image, (classifier.params["window_size"], classifier.params["window_size"])
138
+ )
139
+
140
+ if not classifier.params["color"]:
141
+ image = np.mean(image, axis=2)
142
+
143
+ image = np.expand_dims(image, axis=0)
144
+
145
+ success, prediction = classifier.predict(
146
  image / 255.0,
147
  output_path=args.output_path,
148
  )
149
+
150
+ if success:
151
+ index = np.argmax(prediction)
152
+ logger.info(
153
+ f"prediction: {classifier.class_names[index]} - {prediction[0][index]:.2f}"
154
+ )
155
  elif args.task == "preprocess":
156
  success = preprocess(
157
  args.output_path,
image_classifier/classes.py CHANGED
@@ -102,12 +102,12 @@ class Image_Classifier(object):
102
  )
103
 
104
  if not output_path:
105
- return True
106
 
107
  if not file.save(
108
  f"{output_path}/image_classifier/predictions.pyndarray", predictions
109
  ):
110
- return False
111
 
112
  if test_labels is not None:
113
  from sklearn.metrics import confusion_matrix
@@ -126,7 +126,7 @@ class Image_Classifier(object):
126
  if not file.save(
127
  f"{output_path}/image_classifier/model/confusion_matrix.pyndarray", cm
128
  ):
129
- return False
130
 
131
  if not graphics.render_confusion_matrix(
132
  cm,
@@ -138,7 +138,7 @@ class Image_Classifier(object):
138
  ],
139
  footer=self.signature(prediction_time),
140
  ):
141
- return False
142
 
143
  if test_labels is not None:
144
  logger.info(
@@ -161,7 +161,7 @@ class Image_Classifier(object):
161
  footer=self.signature(prediction_time),
162
  title="distribution of test_labels",
163
  ):
164
- return False
165
 
166
  max_index = test_images.shape[0]
167
  if page_count != -1:
@@ -181,7 +181,7 @@ class Image_Classifier(object):
181
  prediction_time,
182
  )
183
 
184
- return True
185
 
186
  def predict_frame(self, frame):
187
  prediction_time = time.time()
 
102
  )
103
 
104
  if not output_path:
105
+ return True, predictions
106
 
107
  if not file.save(
108
  f"{output_path}/image_classifier/predictions.pyndarray", predictions
109
  ):
110
+ return False, predictions
111
 
112
  if test_labels is not None:
113
  from sklearn.metrics import confusion_matrix
 
126
  if not file.save(
127
  f"{output_path}/image_classifier/model/confusion_matrix.pyndarray", cm
128
  ):
129
+ return False, predictions
130
 
131
  if not graphics.render_confusion_matrix(
132
  cm,
 
138
  ],
139
  footer=self.signature(prediction_time),
140
  ):
141
+ return False, predictions
142
 
143
  if test_labels is not None:
144
  logger.info(
 
161
  footer=self.signature(prediction_time),
162
  title="distribution of test_labels",
163
  ):
164
+ return False, predictions
165
 
166
  max_index = test_images.shape[0]
167
  if page_count != -1:
 
181
  prediction_time,
182
  )
183
 
184
+ return True, predictions
185
 
186
  def predict_frame(self, frame):
187
  prediction_time = time.time()