def read_image(path): try: return preprocess_input( center_crop_and_resize(imread(path)[:, :, :3], image_size=image_size)) except: return None
def aug_images(img_raw, img_size=(299, 299)): (w, h) = img_raw.size if h <= w: b = (w - h) // 2 box_center = (b, 0, h + b, h) box_top = (0, 0, h, h) box_bottom = (w - h, 0, w, h) else: b = (h - w) // 2 box_center = (0, b, w, w + b) box_top = (0, 0, w, w) box_bottom = (0, h - w, w, h) imgs = [ img_raw.resize(img_size, Image.LANCZOS), # img_raw.crop(box_center).resize(img_size, Image.LANCZOS), # img_raw.crop(box_top).resize(img_size, Image.LANCZOS), # img_raw.crop(box_bottom).resize(img_size, Image.LANCZOS), ] imgs_flip = [i.transpose(Image.FLIP_LEFT_RIGHT) for i in imgs] imgs = imgs + imgs_flip imgs_new = [] for img in imgs: imgs_new.append(img) imgs_new.append(img.transpose(Image.ROTATE_90)) imgs_new.append(img.transpose(Image.ROTATE_180)) imgs_new.append(img.transpose(Image.ROTATE_270)) return np.array([efn.preprocess_input(np.array(x)) for x in imgs_new])
def classify_panda(model, image_shape): rgb_panda_image = imread(panda_path) if show_image: bgr_panda_image = cv2.cvtColor(rgb_panda_image, cv2.COLOR_RGB2BGR) cv2.imshow('panda_image', bgr_panda_image) # preprocess input cropped_rgb_panda_image = center_crop_and_resize(rgb_panda_image, image_size=image_shape) if show_image: cropped_bgr_panda_image = cv2.cvtColor( cropped_rgb_panda_image.astype('uint8'), cv2.COLOR_RGB2BGR) cv2.imshow('cropped_panda_image', cropped_bgr_panda_image) preprocessed_rgb_panda_image = preprocess_input(cropped_rgb_panda_image) batch = np.expand_dims(preprocessed_rgb_panda_image, 0) # make prediction and decode predictions = model.predict(batch) xprint(decode_predictions(predictions)) if show_image: while cv2_window_open('panda_image') and cv2_window_open( 'cropped_panda_image'): if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
def preprocess_image(image, image_size): image = io.imread(image) x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) return x
def __image_classifier(response): """ Generates an EfficientNetB4 model and classifies images. Args: response (dict): Query response to send to API. Should be of itemDataType image. """ # Process images for index, item in enumerate(response['data']): image_base64 = item['itemData'] if image_base64 in cache: print(cache[image_base64]) response['data'][index]['ml_suggest'] = cache[image_base64][0][0][1] else: print(response['data'][index]['filename']) image = __image_decoder(image_base64) image = efn.center_crop_and_resize(image=image, image_size=IMG_SIZE) image = efn.preprocess_input(image) img_array = tf.keras.preprocessing.image.img_to_array(image) img_array = tf.expand_dims(img_array, 0) # Predict prediction = model.predict(img_array) prediction = tf.keras.applications.imagenet_utils.decode_predictions(prediction) # Append prediction to response print(prediction) response['data'][index]['ml_suggest'] = prediction[0][0][1] cache[image_base64] = prediction print(response['data'][index]['ml_suggest']) return response
def infer(model, image_path): image = imread(image_path) image_size = model.input_shape[1] x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) predictions = model.predict(x) predictions_with_labels = decode_predictions(predictions) return {t[1]: t[2] for t in predictions_with_labels[0]}
def test_webcams(model, image_shape): for cam_idx in itertools.count(): cap = cv2.VideoCapture(cam_idx + cam_api_reference) if not cap.isOpened(): break ret, bgr_frame = cap.read() xprint("Cam {} image shape: {}".format(cam_idx, bgr_frame.shape)) while True: ret, bgr_frame = cap.read() cv2.imshow('frame', bgr_frame) # Use neural network cam_batch_size = 1 verbose = False # print("model.input_shape:", model.input_shape) # input_size = model.input_shape[2] cropped_bgr_frame = center_crop_and_resize(bgr_frame, image_size=image_shape) cv2.imshow('cropped_frame', cropped_bgr_frame.astype('uint8')) xprint("cropped_bgr_frame.shape:", cropped_bgr_frame.shape) cropped_rgb_frame = cv2.cvtColor(cropped_bgr_frame.astype('uint8'), cv2.COLOR_BGR2RGB) batch = preprocess_input( cropped_rgb_frame.reshape((-1, ) + cropped_rgb_frame.shape)) prediction = model.predict(batch, batch_size=cam_batch_size, verbose=verbose) if use_original_classifier: for decoded_frame_prediction in decode_predictions(prediction): xprint(decoded_frame_prediction) else: if not isinstance(prediction, list): prediction = [prediction] print() for output_head in prediction: for frame_output in output_head: print(frame_output.shape, frame_output) if cv2.waitKey(1) & 0xFF == ord('q'): break if not (cv2_window_open('frame') and cv2_window_open('cropped_frame')): break cap.release() cv2.destroyAllWindows()
def get_frame_batches(video, image_shape, desired_batch_size): video_w = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) video_h = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_rate = video.get(cv2.CAP_PROP_FPS) if ( video_w, video_h ) not in expected_video_resolutions or frame_rate not in expected_frame_rates: xprint("Resolution: {}x{}".format(video_w, video_h)) xprint("Frame rate:", frame_rate) raise Exception("Unexpected video property detected") frame_idx = 0 batch = [] while True: ret, bgr_frame = video.read() if not ret: break else: if show_image and frame_idx % frame_display_stride == 0: image_scale = 0.5 new_width, new_height = bgr_frame.shape[ 1] * image_scale, bgr_frame.shape[0] * image_scale resized_bgr_frame = cv2.resize( bgr_frame, (int(new_width), int(new_height))) cv2.imshow('frame', resized_bgr_frame) cropped_bgr_frame = center_crop_and_resize( bgr_frame, image_size=image_shape, crop_padding=crop_padding) if show_image: cv2.imshow('cropped_frame', cropped_bgr_frame.astype('uint8')) cropped_rgb_frame = cv2.cvtColor(cropped_bgr_frame.astype('uint8'), cv2.COLOR_BGR2RGB) batch.append(preprocess_input(cropped_rgb_frame)) if show_image: if cv2.waitKey(1) & 0xFF == ord('q'): raise Exception("q key pressed during video processing") if not (cv2_window_open('frame') and cv2_window_open('cropped_frame')): raise Exception("Window closed during video processing") if len(batch) == desired_batch_size: yield np.asarray(batch) batch = [] frame_idx += 1 if len(batch) > 0: yield np.asarray(batch) cv2.destroyAllWindows()
def predict(image): x = center_crop_and_resize(image, image_size=image_size) image_modified = x x = preprocess_input(x) x = np.expand_dims(x, 0) # make prediction and decode tb._SYMBOLIC_SCOPE.value = True y = model.predict(x) res = decode_predictions(y) # collect result and the modified image data = {'res': res[0][0], 'new_image': image_modified} return data
def swat_dcnn(input_image, swatdcnn_stage_1, swatdcnn_stage_2, swatdcnn_stage_3): # Load the image. img1 = image.load_img(input_image) plt.imshow(img1) img = image.load_img(input_image, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = efn.preprocess_input(x) # CONDITIONS ################################################################################################# # SWAT-DCNN (Stage-1) swatdcnn_stage_1_result = swatdcnn_stage1(x, swatdcnn_stage_1) ################################################################################################# # SWAT-DCNN (Stage-2) # Run iff Stage-1 result is unhealthy. if swatdcnn_stage_1_result["is_unhealthy"]: swatdcnn_stage_2_result = swatdcnn_stage2(x, swatdcnn_stage_2) else: swatdcnn_stage_2_result = None ################################################################################################# # SWAT-DCNN (Stage-3) # Run iff Stage-2 result is `UNHEALTHY_BSL`. if swatdcnn_stage_2_result and swatdcnn_stage_2_result["disease"] == UNHEALTHY_BSL: swatdcnn_stage_3_result = swatdcnn_stage3(x, swatdcnn_stage_3) else: swatdcnn_stage_3_result = None return { "swatdcnn_stage1": swatdcnn_stage_1_result, "swatdcnn_stage2": swatdcnn_stage_2_result, "swatdcnn_stage3": swatdcnn_stage_3_result }
def preprocess_image(self, inputs): """ Takes as input an image and prepares it for being passed through the network. """ return efn.preprocess_input(inputs)
def split(self, test_size): cant_examples = np.zeros(self.dataset[1]['y'].max() + 1) for i in self.dataset[1]['y']: cant_examples[i] += 1 select = np.where(cant_examples >= (self.dataset[0].shape[0] / self.classes) * test_size) y_new = np.array((), dtype='uint8') pos = np.array((), dtype='uint8') for (k, cla) in enumerate(self.dataset[1]['y']): for j in select[0]: if (cla == j): y_new = np.append(y_new, cla) pos = np.append(pos, k) x_new = np.zeros((len(y_new), self.dataset[0].shape[1], self.dataset[0].shape[2], self.input_shape[2]), dtype='uint8') HEIGHT = 64 WIDTH = 64 if(self.dataset_id=="indianA"): X_new_resize = np.zeros((len(y_new), HEIGHT, WIDTH, self.input_shape[2])) if(self.dataset_id=="indianB"): X_new_resize = np.zeros((len(y_new), HEIGHT, WIDTH, 1)) for (i, index) in enumerate(pos): x_new[i] = self.dataset[0][index] if (self.dataset_id=="indianA" or self.dataset_id=="indianB"): if(self.dataset_id == "indianA"): image = transform.resize(x_new[i], (480, 640), preserve_range=True, mode="reflect", anti_aliasing=True) image = Image.fromarray(image.astype(np.uint8)) left = 20 top = 150.0 right = 550 bottom = 425.0 img = image.crop((left, top, right, bottom)) img2 = np.asarray(img) X_new_resize[i] = transform.resize(img2, (HEIGHT, WIDTH), preserve_range=True, mode="reflect", anti_aliasing=True) else: X_new_resize[i] = transform.resize(x_new[i], (HEIGHT, WIDTH), preserve_range=True, mode="reflect", anti_aliasing=True) if (self.dataset_id=="indianA" or self.dataset_id=="indianB"): X_train, X_test, Y_train, Y_test = sklearn.model_selection.train_test_split(X_new_resize, y_new, test_size=test_size, stratify=y_new) else: X_train, X_test, Y_train, Y_test = sklearn.model_selection.train_test_split(x_new, y_new, test_size=test_size, stratify=y_new) if (X_train.shape[3]==1): X_train = np.repeat(X_train, 3, -1) X_test = np.repeat(X_test, 3, -1) X_train_preprocess = np.zeros((X_train.shape[0], X_train.shape[1], X_train.shape[2], X_train.shape[3]), dtype=np.float32) X_test_preprocess = np.zeros((X_test.shape[0], X_test.shape[1], X_test.shape[2], X_test.shape[3]), dtype=np.float32) for i, x in enumerate(X_train): x = tf.cast(x, tf.float32) x = efn.preprocess_input(x) X_train_preprocess[i] = x for j, xt in enumerate(X_test): xt = tf.cast(xt, tf.float32) xt = efn.preprocess_input(xt) X_test_preprocess[j] = xt return X_train_preprocess, X_test_preprocess, Y_train, Y_test
#name: Image Classification #description: Image classification based of Efficientnet model #language: python #input: file file #output: map classes [Detected classes with probabilities] #tags: demo, panel, files, efficientnet #condition: file.isFile && file.size < 1e6 && (file.name.endsWith("jpg") || file.name.endsWith("jpeg") || file.name.endsWith("png")) #help-url: https://github.com/qubvel/efficientnet import numpy as np from skimage.io import imread from efficientnet.keras import EfficientNetB0 from keras.applications.imagenet_utils import decode_predictions from efficientnet.keras import center_crop_and_resize, preprocess_input image = imread(file) model = EfficientNetB0(weights='imagenet') image_size = model.input_shape[1] _image = center_crop_and_resize(image, image_size=image_size) _image = preprocess_input(_image) _image = np.expand_dims(_image, 0) predicted = model.predict(_image) predicted = decode_predictions(predicted)[0] classes = {} for p in predicted: classes[p[1]] = float(p[2])
def preprocess_img(x): return efn.preprocess_input(x)
import sys import numpy as np from skimage.io import imread sys.path.append('..') from keras.applications.imagenet_utils import decode_predictions from efficientnet.keras import EfficientNetB0 from efficientnet.keras import center_crop_and_resize, preprocess_input model = EfficientNetB0(weights='imagenet') image = imread('../misc/panda.jpg') image_size = model.input_shape[1] x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) y = model.predict(x) print(decode_predictions(y))
new_img = cv2.bitwise_not(new_img) img = img[0] img = (img*255).astype('uint8') img = cv2.add(img,new_img) img[img == new_img] = 0 test_image = Image.fromarray(img).convert('RGB') #each model prediction mean resized_image = test_image.resize((efficient_input_size,efficient_input_size)) resized_image = np.array(resized_image,'uint8') ######################################################################## resized_image = preprocess_input(resized_image) resized_image = np.expand_dims(resized_image,axis=0) prediction = efficient.predict(resized_image)[0] ######################################################################## print(prediction) print(prediction.argmax()) print(prediction.max()) output = efficient.output[:,prediction.argmax()] last_conv_layer = efficient.get_layer('top_conv') grads = K.gradients(output,last_conv_layer.output)[0] pooled_grads = K.mean(grads,axis=(0,1,2))
def resol(x): a = cv2.imread(x) a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB) image = cv2.resize(a, (300, 300)) c = np.expand_dims(image, 0) return (efn.preprocess_input(c))