def test_train_dev_split(input_data, output_data, train=0.8, dev=0.1, test=0.1): #make seed for exact results everything #input_data=preprocess_input(input_data) input_data, output_data = shuffle(input_data, output_data, random_state=0) split1 = int(train * len(input_data)) split2 = int((train + dev) * len(input_data)) train_input = input_data[:split1] dev_input = input_data[split1:split2] test_input = input_data[split2:] train_output = output_data[:split1] dev_output = output_data[split1:split2] test_output = output_data[split2:] train_input = model2.predict( preprocess_input( np.array([np.array(i.resize((224, 224))) for i in train_input]))) dev_input = model2.predict( preprocess_input( np.array([np.array(i.resize((224, 224))) for i in dev_input]))) test_input = model2.predict( preprocess_input( np.array([np.array(i.resize((224, 224))) for i in test_input]))) print(train_input[0]) return train_input, np.array(train_output), dev_input, np.array( dev_output), test_input, np.array(test_output)
def predict(self, frame): # expand 3D RGB frame into 4D "batch" sample = np.expand_dims(frame, axis=0) processed_sample = preprocess_input(sample.astype(np.float32)) features = self.conv_base.predict(processed_sample) decoded_features = decode_predictions(features) return decoded_features
def main(): model = create_model() model.load_weights(WEIGHTS_FILE) for filename in glob.glob(IMAGES): unscaled = cv2.imread(filename) image_height, image_width, _ = unscaled.shape image = cv2.resize(unscaled, (IMAGE_SIZE, IMAGE_SIZE)) feat_scaled = preprocess_input(np.array(image, dtype=np.float32)) region, class_id = model.predict(x=np.array([image])) region = region[0] x0 = int(region[0] * image_width / IMAGE_SIZE) y0 = int(region[1] * image_height / IMAGE_SIZE) x1 = int((region[0] + region[2]) * image_width / IMAGE_SIZE) y1 = int((region[1] + region[3]) * image_height / IMAGE_SIZE) class_id = np.argmax(class_id, axis=1)[0] cv2.rectangle(unscaled, (x0, y0), (x1, y1), (0, 0, 255), 1) cv2.putText(unscaled, "class: {}".format(class_names[class_id]), (x0, y0), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA) cv2.imshow("image", unscaled) cv2.waitKey(0) cv2.destroyAllWindows()
def visualize(self, img_path): img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = self.model.predict(x) # Hotmap pred_id = np.argmax(preds[0]) predicted_output = self.model.output[..., pred_id] last_conv_layer = self.model.get_layer('Conv_1') grads = K.gradients(predicted_output, last_conv_layer.output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([self.model.input], [pooled_grads, last_conv_layer.output[0]]) pooled_grads_value, conv_layer_output_value = iterate([x]) for i in range(1280): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] heatmap = np.mean(conv_layer_output_value, axis=-1) heatmap /= np.max(heatmap) # Show on picture img = cv2.imread(img_path) heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) heatmap = np.uint8(255 * heatmap) heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) superimposed_img = heatmap * 0.4 + img b, g, r = cv2.split(superimposed_img) superimposed_img = cv2.merge([r, g, b]) return superimposed_img
def predict_object(msg, cam, model, ja_labels): img_shape = (224, 224) img_path = "/tmp/cap.jpg" save_captured_image(img_path, cam) img = image.load_img(img_path, target_size=img_shape) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # predict preds = model.predict(x) # decode results = decode_predictions(preds, top=1)[0] for r in results: print('Predicted:', r) en_wnid = r[0] acc = r[2] # translate english label to japanese one. ja_label = en2ja_label(ja_labels, en_wnid, acc) print(ja_label) if ja_label != "": call_jtalk(ja_label)
def __init__(self, csv_file, batch_size=32, classes=CLASSES, inmemory=False): self.paths = [] self.batch_size = batch_size self.inmemory = inmemory with open(csv_file, "r") as file: self.y = np.zeros((sum(1 for line in file), 4 + classes)) file.seek(0) reader = csv.reader(file, delimiter=",") for index, (path, x0, y0, x1, y1, _, class_id) in enumerate(reader): self.y[index][0] = x0 self.y[index][1] = y0 self.y[index][2] = x1 self.y[index][3] = y1 self.y[index][min(4 + int(class_id), self.y.shape[1] - 1)] = 1 self.paths.append(path) if self.inmemory: self.x = self.__load_images(self.paths) self.x = preprocess_input(self.x)
def predict_image(self, model, cv2_img=None, path=None): if cv2_img.all() != None: im = cv2_img else: if path != None: im = cv2.imread(path) else: print("Predict Image had no path image or image CV2") return None if im.shape[0] != self._image_size: im = cv2.resize(im, (self._image_size, self._image_size)) """ self.rgb_camera_object.display_image( image_display=im, life_time_ms=50, name="ResizedCAM" ) """ image = np.array(im, dtype='f') image = preprocess_input(image) self.rgb_camera_object.display_image(image_display=image, life_time_ms=50, name="ImagePredict") prediction = model.predict(x=np.array([image]))[0] return prediction
def vectorize_add(dir_name, id_part): print("Vectorise") K.clear_session() # model = MobileNetV2(input_shape=(224, 224, 3), include_top=False, pooling='max',alpha=1.4) keras_model_name = r"C:\Users\HOME\PycharmProjects\main_server\model_MIRO_M2_89.h5" base = load_model(keras_model_name) model = Model(inputs=base.input, outputs=base.get_layer(index=-2).output) vec_arr = [] print(os.path.join(dir_name, str(id_part))) for top, dirs, files in os.walk(os.path.join(dir_name, str(id_part))): for nm in files: img = image.load_img(os.path.join(top, nm), target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) pred = model.predict(x) listdata = pred.ravel().tolist() vec_arr.append(listdata) id_vec = coll.insert({"vec_part": vec_arr, "id_part": str(id_part)}) vec_arr.clear() print('Finished') return id_vec
def upload_file(): if request.method == 'POST': f = request.files["file"] path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename) base_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet') top = tf.keras.Sequential([ tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(1, activation='sigmoid') ]) top.set_weights('my_model_weights.h5') model = tf.keras.Sequential([base_model, top]) img = image.load_img(path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(img) # preds_decoded = decode_predictions(preds, top=3)[0] # print(preds_decoded) f.save(path) return render_template('index.html', title='Success', predictions=preds, user_image=f.filename)
def load_image(path): # img_path = sys.argv[1] img_path = path img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
def infer(self, image): image_data = preprocess_input( np.expand_dims(np.asarray(image.resize((224, 224)), dtype=np.float32), axis=0)) features = self.feature_extractor.predict(image_data)[0][0][0] state_action, action_id = self.agent.choose_action(features) return state_action, features, action_id, int( [i for i in np.arange(5, 105, 10)][action_id])
def preprocess_image(network_name, x): if (network_name == "ResNet50"): x = resnet50.preprocess_input(x) elif (network_name == "MobileNetV2"): x = mobilenetv2.preprocess_input(x) elif (network_name == "VGG19"): x = vgg19.preprocess_input(x) elif (network_name == "SqueezeNet"): x = imagenet_utils.preprocess_input(x) return x
def preprocess_image(image, target_size): if image.mode != "RGB": image = image.convert("RGB") image = image.resize(target_size) image = keras_image.img_to_array(image) # print(image) # print('shape of image: ', np.shape(image)) image = np.expand_dims(image, axis=0) image = preprocess_input(image) return image
def predict_image(path, model): im = cv2.imread(path) if im.shape[0] != IMAGE_SIZE: im = cv2.resize(im, (IMAGE_SIZE, IMAGE_SIZE)) image = np.array(im, dtype='f') image = preprocess_input(image) region = model.predict(x=np.array([image]))[0] return convert_coords(*region)
def __getitem__(self, idx): batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size] if self.inmemory: batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size] return batch_x, [batch_y[..., :4], batch_y[..., 4:]] batch_x = self.paths[idx * self.batch_size:(idx + 1) * self.batch_size] images = self.__load_images(batch_x) images = preprocess_input(images) return images, [batch_y[..., :4], batch_y[..., 4:]]
def run(self, img_path=None, img=None): if img is None and img_path is None: return None elif img is None: img = image.load_img(img_path, target_size=self.input_shape[:2]) x = image.img_to_array(img) else: img = cv2.resize( img, self.input_shape[:2], interpolation=cv2.INTER_CUBIC) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return self.model.predict(x)
def __init__(self, csv_file, batch_size=32, inmemory=False, number_of_elements_to_be_output=2): self._number_of_elements_to_be_output = number_of_elements_to_be_output assert (self._number_of_elements_to_be_output == 2 ), "We only support 2 output model XY" self.paths = [] self.batch_size = batch_size self.inmemory = inmemory with open(csv_file, "r") as file: """ Which element do we have interest in? [absolute_original_path, width, height, x_com, y_com, z_com, quat_x, quat_y, quat_z, quat_w, class_name, class_names[class_name]] We only want the dcnn to learn to give us the XYZ 3D position of the ObjectTo learn. x_com y_com z_com """ self.y = np.zeros( (sum(1 for line in file), self._number_of_elements_to_be_output)) file.seek(0) reader = csv.reader(file, delimiter=",") for index, (scaled_img_path, _, _, x_com, y_com, z_com, _, _, _, _, _, _) in enumerate(reader): """ if self._number_of_elements_to_be_output == 3: self.y[index][0] = x_com self.y[index][1] = y_com self.y[index][2] = z_com """ if self._number_of_elements_to_be_output == 2: self.y[index][0] = x_com self.y[index][1] = y_com self.paths.append(scaled_img_path) print(str(self.y)) #print (str(self.paths)) if self.inmemory: self.x = self.__load_images(self.paths) self.x = preprocess_input(self.x)
def fn_reader(d, path): size = (224, 224) if d["type"] == "real": image = np.array( Image.open(path + "/data/original/p3/" + d["name"]).resize(size)).astype(np.float32) if d["type"] == "pggan" or d["type"] == "stylegan": image = np.array( Image.open(path + "/result_gan/" + str(d["type"]) + "/" + d["name"]).resize(size)).astype(np.float32) imgs = [preprocess_input(light_augmentation(image))] labels = [to_categorical(int(d["label"]), num_classes=2)] return imgs, labels
def predict(file_path): _, file_ext = os.path.splitext(file_path) img = scipy.misc.imresize(plt.imread(file_path, format=file_ext), (image_size, image_size)) img = np.array(img, dtype=np.float32) if img.shape[-1] < 3 or len(img.shape) != 3: img = np.stack((img, ) * 3, -1) img = np.expand_dims(img, axis=0) if img.shape[-1] == 4: img = img[:, :, :, :-1] img = preprocess_input(img) with graph.as_default(): bottleneck_features = extractor_model.predict(img) result = top_model.predict(bottleneck_features) return result[0][0]
def my_datagen(X, y, batch_size, keras_datagen=None): if keras_datagen is None: use_keras_datagen = False else: use_keras_datagen = True while True: for X_batch, y_batch in get_batches(X, y, batch_size): if use_keras_datagen: yield next( keras_datagen.flow(X_batch, y_batch, shuffle=False, batch_size=batch_size)) else: yield preprocess_input(X_batch), y_batch
def image_extract(folder_path, number_of_images=800): images = [None] * number_of_images for filename in os.listdir(folder_path): if filename.endswith(".png"): pos = filename.find("_") id = int(filename[:pos]) img = load_img(os.path.join(folder_path, filename), target_size=(224, 224)) images[id] = img_to_array(img) # images[id] = np.expand_dims(images[id], axis=0) images[id] = preprocess_input(images[id]) # print(np.shape(images[id])) # print("Shape: {}".format(np.shape(images[1]))) images = list(filter(None.__ne__, images)) return images
def main(): model = create_model() model.load_weights(WEIGHTS_FILE) for filename in glob.glob(IMAGES): unscaled = cv2.imread(filename) image_height, image_width, _ = unscaled.shape image = cv2.resize(unscaled, (IMAGE_SIZE, IMAGE_SIZE)) feat_scaled = preprocess_input(np.array(image, dtype=np.float32)) region = model.predict(x=np.array([feat_scaled]))[0] x0 = int(region[0] * image_width / IMAGE_SIZE) y0 = int(region[1] * image_height / IMAGE_SIZE) x1 = int((region[0] + region[2]) * image_width / IMAGE_SIZE) y1 = int((region[1] + region[3]) * image_height / IMAGE_SIZE) print(x0, y0, x1, y1)
def classify_cam(self): #-------------------read image from camera--------------------- #self.camera.capture(self.rawCapture, format="bgr") #image = self.rawCapture.array image = cv2.imread("test3.jpg", cv2.IMREAD_COLOR) #read image #-------------------pre-process imagae--------------------------- width = 224 height = 224 dim = (width, height) image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) image = preprocess_input(image) input_data = np.empty((1, 224, 224, 3)) input_data[0] = image #-------------------classify processed image----------------------- print(np.argmax(self.model.predict(input_data))) if np.argmax(self.model.predict(input_data)) == self.target_index: return 1 else: return 0
def main(): model = create_model() model.load_weights(WEIGHTS_FILE) for filename in glob.glob(IMAGES): unscaled = cv2.imread(filename) image_height, image_width, _ = unscaled.shape image = cv2.resize(unscaled, (IMAGE_SIZE, IMAGE_SIZE)) feat_scaled = preprocess_input(np.array(image, dtype=np.float32)) region = model.predict(x=np.array([feat_scaled]))[0] x0 = int(region[0] * image_width / IMAGE_SIZE) y0 = int(region[1] * image_height / IMAGE_SIZE) x1 = int((region[0] + region[2]) * image_width / IMAGE_SIZE) y1 = int((region[1] + region[3]) * image_height / IMAGE_SIZE) cv2.rectangle(unscaled, (x0, y0), (x1, y1), (0, 0, 255), 1) cv2.imshow("image", unscaled) cv2.waitKey(0) cv2.destroyAllWindows()
def main(args): # load the classification model classification_graph = load_graph(args.model) classification_session = tf.Session(graph=classification_graph) # load image or image dir if os.path.isdir(args.input): img_list = glob.iglob(args.input + '/*.' + args.image_ext) else: img_list = [args.input] img_list = sorted(img_list) for i, img_name in enumerate(img_list): image_org = image.load_img(img_name, target_size=(args.image_height, args.image_width)) img = image.img_to_array(image_org) if args.backbone == 'ResNet50': img = resnet50.preprocess_input(img) elif args.backbone == 'MobileNetV2': img = mobilenetv2.preprocess_input(img) else: print('Unsupported backbone') continue # process image start = time.time() image_tensor = classification_graph.get_tensor_by_name('input_1:0') output_tensor = classification_graph.get_tensor_by_name( 'dense_2/Softmax:0') scores = classification_session.run( [output_tensor], feed_dict={image_tensor: np.expand_dims(img, axis=0)}) print(img_name) print("processing time: ", time.time() - start) print(np.round(scores[0], 3))
model_input_path = os.path.join(execution_path, 'MobileNetV2_ImageNet.h5') #model_output_path = os.path.join(execution_path, 'MobileNetV2_ImageNet.tflite') input_image_path = os.path.join(execution_path, 'images/magpie.jpg') # Lets optimize the model for the Jetson's GPU input_model = load_model(model_input_path) frozenmodel = FrozenGraph(input_model, (224, 224, 3)) print('FrozenGraph build.') model = TftrtEngine(frozenmodel, 1, 'FP16', output_shape=(1000)) #model = TftrtEngine(frozenmodel, 1, 'INT8', output_shape=(1000)) print('TF-TRT model ready to rumble!') # Load and preprocess the image input_image = PIL.Image.open(input_image_path) input_image = np.asarray(input_image) preprocessed = preprocess_input(input_image) preprocessed = np.expand_dims(preprocessed, axis=0) print('input tensor shape : ' + str(preprocessed.shape)) # This actually calls the inference print("Warmup prediction") output = model.infer(preprocessed) print(decode_predictions(output)) time.sleep(1) print("starting now (Jetson Nano)...") s = time.time() for i in range(0,250,1):
def preprocess_and_filter(image): image = filterImages(image) return preprocess_input(image)
for idx, image_batch in enumerate(image_data_batchs): performance = defaultdict(list) print("\n\nbatch %s:" % (idx + 1)) curr_label_batch = image_label_batchs[idx] curr_bm_size_batch = ref_size_batchs[idx] acc_list = [] size_list = [] banchmark_size = [] for img_id, image in enumerate(image_batch): label = curr_label_batch[img_id] image_data = preprocess_input( np.expand_dims(np.asarray(image.resize((224, 224)), dtype=np.float32), axis=0)) features = np.array([0, 1]) if np.argmax( feature_extractor.predict(image_data)) == 1 else np.array( [1, 0]) step_count += 1 # action_id = agent.choose_action(features) action_id = 2 if np.argmax(features) == 0 else 5 action = [i for i in np.arange(5, 105, 10)][action_id] processed_img, size = env.process_single_image(image, int(action)) _, ref_size = env.process_single_image(image, 75) accuracy = env.deep_model.evaluate(x=np.expand_dims(processed_img, axis=0),
early_stop = EarlyStopping(monitor='val_acc', min_delta=1e-5, patience=30, verbose=1, mode='auto') reduce_LR = ReduceLROnPlateau(monitor='acc', factor=0.8, patience=5, mode='auto', verbose=1) if __name__ == '__main__': train_features = np.array([ preprocess_input( np.asarray(Image.open(path).convert("RGB").resize((224, 224)), dtype=np.float32)) for path in train_img_paths ]) test_features = np.array([ preprocess_input( np.asarray(Image.open(path).convert("RGB").resize((224, 224)), dtype=np.float32)) for path in test_img_paths ]) base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3)) for layer in base_model.layers: layer.trainable = False for layer in base_model.layers[81:98]: layer.trainable = True
if __name__ == '__main__': # Load images from 'frame_train' dir img_dic = {} for hand_sign in const.HAND_SIGNS: imgs_fpath = glob.glob(os.path.join(const.FRAME_TRAIN_DIR.format(hand_sign), "*.jpg")) img_dic.update({ hand_sign: imgs_fpath }) print(len(imgs_fpath)) # Create model model_dlv3 = model.Deeplabv3() # Mask image for hand_sign, imgs_fpath in img_dic.items(): const.check_folder_existence(const.MASK_DIR.format(hand_sign)) for img_fpath in imgs_fpath: img = preprocess_input(cv2.imread(img_fpath).astype("float")) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (const.MASK_WIDTH, const.MASK_HEIGHT)) predicted = model_dlv3.predict(img[np.newaxis, ...]) # Get only person and background back_score = predicted[0,:,:,0] person_score = predicted[0,:,:,15] # Mask only background and person mask = (person_score > back_score).astype("uint8")*255 img_name = img_fpath.split('/')[-1] save_folder = const.MASK_SAVE_DIR.format(hand_sign, img_name) print(save_folder) cv2.imwrite(save_folder, mask)