def get_image(size): """Returns an image loaded into an np.ndarray with dims [1, size, size, 3]. Args: size: Size of image. Returns: np.ndarray. """ img_filename = _resource_loader.get_path_to_datafile( "testdata/grace_hopper.jpg") img = image.load_img(img_filename, target_size=(size, size)) img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) return img_array
def test_sample_movie_data_generator(self): frames = 24 for test_images in _generate_test_images(): img_list = [] for im in test_images: frame_list = [] for _ in range(frames): frame_list.append(img_to_array(im)[None, ...]) img_stack = np.vstack(frame_list) img_list.append(img_stack) images = np.vstack(img_list) batch_count = images.shape[0] // frames images = np.reshape(images, (batch_count, frames, *images.shape[1:])) generator = image_generators.SampleMovieDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, shear_range=0.5, zoom_range=0.2, channel_shift_range=1., brightness_range=(1, 5), fill_mode='nearest', cval=0.5, horizontal_flip=True, vertical_flip=True) img_w, img_h = 21, 21 win_x, win_y, win_z = 2, 2, 2 test_batches = 8 # Basic test before fit train_dict = { 'X': np.random.random((test_batches, frames, img_w, img_h, 3)), 'y': np.random.randint(2, size=(test_batches, frames, img_w, img_h, 1)) } generator.flow(train_dict, window_size=(win_x, win_y, win_z)) # Temp dir to save generated images temp_dir = self.get_temp_dir() # Fit assert generator.random_transform( images[0]).shape == images[0].shape generator.fit(images, augment=True, seed=1) train_dict['X'] = images test_shape = (images.shape[0], *images.shape[1:-1], 1) train_dict['y'] = np.random.randint(2, size=test_shape) for x, _ in generator.flow(train_dict, save_to_dir=temp_dir, window_size=(win_x, win_y, win_z), balance_classes=True, max_class_samples=100, shuffle=True): shape = (2 * win_z + 1, 2 * win_x + 1, 2 * win_y + 1, x.shape[-1]) self.assertEqual(x.shape[1:], shape) break
last_conv = model.get_layer('block5_conv3') from tensorflow.python.keras import backend as K grads = K.gradients(model_output, last_conv.output)[0] #最終畳み込み層の勾配 pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([model.input], [pooled_grads, last_conv.output[0]]) from tensorflow.python.keras.preprocessing import image import numpy as np img_path = './living_room/living_room_0019.jpg' #予測する画像 img_keras = image.load_img(img_path, target_size=(224, 224)) img_tensor = image.img_to_array(img_keras) img_tensor = np.expand_dims(img_tensor, axis=0) predicts = model.predict(img_tensor, batch_size=16, verbose=1, steps=None) scenes_num = np.argmax(predicts) scenes = [ "bathroom", "bedroom", "book_shelf", "classroom", "dining_room", "dish_shelf", "entrance", "gameroom", "gym", "japanese_room", "kids_room", "kitchen", "library", "living_room", "meeting_room", "restaurant", "shoes_box", "steps", "toilet", "working_desk" ] print("The prediction is {}".format(scenes[scenes_num])) #予測結果の出力 # モデルの訓練時と同じ方法で前処理 img_tensor /= 255. pooled_grads_val, conv_output_val = iterate(
def ImageEncode(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) return x
def predictMultipleImages(self, sent_images_array, result_count_per_image=1, input_type="file"): """ 'predictMultipleImages()' function is used to predict more than one image by receiving the following arguments: * input_type , the type of inputs contained in the parsed array. Acceptable values are "file", "array" and "stream" * sent_images_array , an array of image file paths, image numpy array or image file stream * result_count_per_image (optionally) , the number of predictions to be sent per image, which must be whole numbers between 1 and the number of classes present in the model This function returns an array of dictionaries, with each dictionary containing 2 arrays namely 'prediction_results' and 'prediction_probabilities'. The 'prediction_results' contains possible com.ml.objects classes arranged in descending of their percentage probabilities. The 'prediction_probabilities' contains the percentage probability of each object class. The position of each object class in the 'prediction_results' array corresponds with the positions of the percentage possibilities in the 'prediction_probabilities' array. :param input_type: :param sent_images_array: :param result_count_per_image: :return output_array: """ output_array = [] for image_input in sent_images_array: prediction_results = [] prediction_probabilities = [] if (self.__modelLoaded == False): raise ValueError("You must call the loadModel() function before making predictions.") else: if (self.__modelType == "squeezenet"): from .custom_utils import preprocess_input from .custom_utils import decode_predictions if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=( self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") model = self.__model_collection[0] prediction = model.predict(image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image), model_json=self.jsonPath) for result in predictiondata: prediction_results.append(str(result[0])) prediction_probabilities.append(str(result[1] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "resnet"): model = self.__model_collection[0] from .custom_utils import preprocess_input from .custom_utils import decode_predictions if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=( self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image), model_json=self.jsonPath) for result in predictiondata: prediction_results.append(str(result[0])) prediction_probabilities.append(str(result[1] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "densenet"): model = self.__model_collection[0] from .custom_utils import preprocess_input from .custom_utils import decode_predictions from ..DenseNet.densenet import DenseNetImageNet121 if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=( self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image), model_json=self.jsonPath) for result in predictiondata: prediction_results.append(str(result[0])) prediction_probabilities.append(str(result[1] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "inceptionv3"): model = self.__model_collection[0] from com.ml.objects.Prediction.InceptionV3.inceptionv3 import InceptionV3 from .custom_utils import decode_predictions, preprocess_input if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=( self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image), model_json=self.jsonPath) for result in predictiondata: prediction_results.append(str(result[0])) prediction_probabilities.append(str(result[1] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) return output_array
img_path = r"D:\参赛文件\cats_and_dogs_small\test\cats\cat.1700.jpg" from tensorflow.python.keras.preprocessing import image import numpy as ny img = image.load_img(img_path, target_size=(150, 150)) img_tensor = image.img_to_array(img) img_tensor = ny.expand_dims(img_tensor, axis=0) img_tensor /= 255. print(img_tensor.shape) # import matplotlib.pyplot as plt # plt.imshow(img_tensor[0]) # plt.show() from tensorflow.python.keras import models origenal_mode_path = "E:\\1- data\\2.h5" model = models.load_model(origenal_mode_path) layer_outputs = [layer.output for layer in model.layers[:8]] activation_model = models.Model(inputs=model.input, outputs=layer_outputs) activations = activation_model.predict(img_tensor) first_layer_activation = activations[0] import matplotlib.pyplot as plt plt.matshow(first_layer_activation[0, :, :, 7], cmap='viridis') plt.show()
def main_train(self): with tf.Graph().as_default(): with tf.Session() as sess: img_data = facenet.get_dataset(self.datadir) path, label = facenet.get_image_paths_and_labels(img_data) print("label") print(label) print('Classes: %d' % len(img_data)) print('Images: %d' % len(path)) facenet.load_model(self.modeldir) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] print('Extracting features of images for model') batch_size = 1000 image_size = 160 nrof_images = len(path) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) #print(nrof_batches_per_epoch) #for i in range(nrof_batches_per_epoch): start_index = 0 * batch_size end_index = min((0 + 1) * batch_size, nrof_images) paths_batch = path[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) class_names = [cls.name.replace('_', ' ') for cls in img_data] classifier_file_name = os.path.expanduser( self.classifier_filename) print('emb_array') print(emb_array) X_embedded = TSNE(n_components=2).fit_transform(emb_array) X_embedded -= X_embedded.min(axis=0) X_embedded /= X_embedded.max(axis=0) print("X_embedded") print(X_embedded) #for i in range(0, nrof_images-1): # plt.plot(X_embedded[i, 0], X_embedded[i, 1],'bo') plt.legend(bbox_to_anchor=(1, 1)) plt.show() out_dim = 8 out_res = 160 to_plot = np.square(out_dim) grid = np.dstack( np.meshgrid(np.linspace(0, 1, out_dim), np.linspace(0, 1, out_dim))).reshape(-1, 2) cost_matrix = cdist(grid, X_embedded, "sqeuclidean").astype(np.float32) cost_matrix = cost_matrix * (100000 / cost_matrix.max()) print(cost_matrix) #rids, cids = solve_dense(costs) #print(rids) row_ind, col_ind = linear_sum_assignment(cost_matrix) row_asses, col_asses, _ = lapjv(cost_matrix) print("To cos") print(col_asses) print("teraz to!") print(row_ind) print(col_ind) for r, c in zip(row_ind, col_asses): print(r, c) # Row/column pairings grid_jv = grid[col_asses] out = np.ones((out_dim * out_res, out_dim * out_res, 3)) print(grid_jv) for pos, img in zip(grid_jv, images[0:to_plot]): h_range = int(np.floor(pos[0] * (out_dim - 1) * out_res)) w_range = int(np.floor(pos[1] * (out_dim - 1) * out_res)) out[h_range:h_range + out_res, w_range:w_range + out_res] = image.img_to_array(img) print(out) im = image.array_to_img(out) im.save("obrazek.jpg", quality=95)
def load_and_process_image(image_path): img = load_img(image_path, target_size=(img_height, img_weidth)) img = img_to_array(img) img = preprocess_input(img) img = np.expand_dims(img, axis=0) return img
target_size=(224, 224), class_mode='binary', batch_size=16, seed=1) #1エポックで何バッチ文学習 history = model.fit_generator(train_itr, steps_per_epoch=math.ceil(train_itr.samples / 16), epochs=5) #Image予測 #画像ロード predit_img = load_img(testImgPath, target_size=(224, 224)) predict_ndarr = img_to_array(predit_img) #前処理 test_img = preprocess_input(predict_ndarr) arr_input = np.stack([test_img]) #画像予測 probs = model.predict(arr_input) #probs test_img_dir = 'C:/Users/user/Desktop/workspace/doc/sysp/img/unknow/' x_text, true_labels = load_random_imgs(test_img_dir, seed=1) probs = model.predict(x_text) probs
def write(): st.title(' Face Mask Detector') net = load_face_detector_and_model() model = load_cnn_model() selected_option = st.radio("Choose", ('File', 'Webcam')) if selected_option == 'File': #uploaded_image = st.sidebar.file_uploader("Choose a JPG file", type="jpg") uploaded_image = st.sidebar.file_uploader("Choose a JPG file", type=FILE_TYPES) confidence_value = st.sidebar.slider('Confidence:', 0.0, 1.0, 0.5, 0.1) if uploaded_image: image1 = Image.open(uploaded_image) st.sidebar.image(image1, caption='Uploaded Image.', use_column_width=True) #st.sidebar.info('Uploaded image:') #st.sidebar.image(uploaded_image, width=240) #f = open(uploaded_image, 'rb') #file = st.file_uploader("Upload file", type=FILE_TYPES) show_file = st.empty() if not uploaded_image: show_file.info("Please upload a file of type: " + ", ".join(FILE_TYPES)) return file_type = get_file_type(uploaded_image) if file_type == FileType.IMAGE: show_file.image(image1) elif file_type == FileType.PYTHON: st.code(uploaded_image.getvalue()) else: data = pd.read_csv(uploaded_image) st.dataframe(data.head(10)) f = open(get_file_name(uploaded_image), 'rb') img_bytes = f.read() f.close() grad_cam_button = st.sidebar.button('Grad CAM') patch_size_value = st.sidebar.slider('Patch size:', 10, 90, 20, 10) occlusion_sensitivity_button = st.sidebar.button( 'Occlusion Sensitivity') image = cv2.imdecode(np.fromstring(img_bytes, np.uint8), 1) #image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), 1) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) orig = image.copy() (h, w) = image.shape[:2] blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0)) net.setInput(blob) detections = net.forward() for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > confidence_value: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) face = image[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) expanded_face = np.expand_dims(face, axis=0) (mask, withoutMask) = model.predict(expanded_face)[0] predicted_class = 0 label = "No Mask" if mask > withoutMask: label = "Mask" predicted_class = 1 color = (0, 255, 0) if label == "Mask" else (0, 0, 255) label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100) cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(image, (startX, startY), (endX, endY), color, 2) st.image(image, width=640) st.write('### ' + label) if grad_cam_button: data = ([face], None) explainer = GradCAM() grad_cam_grid = explainer.explain(data, model, class_index=predicted_class, layer_name="Conv_1") st.image(grad_cam_grid) if occlusion_sensitivity_button: data = ([face], None) explainer = OcclusionSensitivity() sensitivity_occlusion_grid = explainer.explain( data, model, predicted_class, patch_size_value) st.image(sensitivity_occlusion_grid) # PROGRAM FOR WEB CAM if selected_option == 'Webcam': labels_dict = {0: 'without_mask', 1: 'with_mask'} color_dict = {0: (0, 0, 255), 1: (0, 255, 0)} size = 4 webcam = cv2.VideoCapture(0) #Use camera 0 st.write("Webcam On") stframe_cam = st.empty() # We load the xml file classifier = cv2.CascadeClassifier( 'src/pages/Services/frecog/haarcascade_frontalface_default.xml') while True: (rval, im) = webcam.read() stframe_cam.image(im) # st.write("Webcam Read") #if im: #ret, framecar = vf.read() im = cv2.flip(im, 1, 1) #Flip to act as a mirror # Resize the image to speed up detection mini = cv2.resize(im, (im.shape[1] // size, im.shape[0] // size)) # detect MultiScale / faces faces = classifier.detectMultiScale(mini) # Draw rectangles around each face for f in faces: (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup #Save just the rectangle faces in SubRecFaces face_img = im[y:y + h, x:x + w] resized = cv2.resize(face_img, (150, 150)) normalized = resized / 255.0 reshaped = np.reshape(normalized, (1, 150, 150, 3)) reshaped = np.vstack([reshaped]) result = model.predict(reshaped) #print(result) label = np.argmax(result, axis=1)[0] cv2.rectangle(im, (x, y), (x + w, y + h), color_dict[label], 2) cv2.rectangle(im, (x, y - 40), (x + w, y), color_dict[label], -1) cv2.putText(im, labels_dict[label], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2) # Show the image stframe_cam.image('LIVE', im) #cv2.imshow('LIVE', im) key = cv2.waitKey(10) # if Esc key is press then break out of the loop if key == 27: #The Esc key break # Stop video webcam.release() # Close all started windows cv2.destroyAllWindows() #write() #uploaded_image.close()
def rotations(): datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') return datagen train_cats_dir = "D:\\参赛文件\\cats_and_dogs_small\\train\\cats" fnames = [ os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir) ] img_path = fnames[1] #选择一张图像进行增强 img = image.load_img(img_path, target_size=(150, 150)) #读取图像并调整大小 x = image.img_to_array(img) #将其转换为形状(150,150,3)的数组 x = x.reshape((1, ) + x.shape) i = 0 import matplotlib.pyplot as plt for batch in rotations().flow(x, batch_size=1): plt.figure(i) imgplot = plt.imshow(image.array_to_img(batch[0])) i += 1 if i % 4 == 4: break plt.show()
def load_image(image_dir): image = load_img(image_dir, target_size=(224, 224)) image = img_to_array(image) image = image / 255 return image
def __data_generation(self, counting_dataset_temp, labels_temp, list_ranking_imgs_temp): 'Generates data containing batch_size samples' # counting batch X_counting = np.empty((self.batch_size, *self.dim, self.n_channels)) # ranking batch X_ranking = np.empty((self.batch_size, *self.dim, self.n_channels)) # counting batch target y_counting = np.empty((self.batch_size, 14, 14, 1)) # to temporarily save the resized counting target y_tmp = np.empty((self.batch_size, 14, 14)) #Generate counting batch for i, counting_img in enumerate(counting_dataset_temp): width, height = counting_img.size ####From PAPER: During training we sample one sub-image from each training image per epoch #From PAPER: To further improve the performance of our baseline network, we introduce multi-scale sampling from the available #labeled datasets. Instead of using the whole image as an input, we randomly sample square patches of varying size (from 56 to 448 pixels). random_size = random.randint(min(56, width, height), min(448, width, height)) #select random size random_x1 = random.randint( 0, width - random_size ) #select random point (the left-top corner of the patch) random_y1 = random.randint(0, height - random_size) crop_img = counting_img.crop( (random_x1, random_y1, random_x1 + random_size, random_y1 + random_size)) crop_resized_img = crop_img.resize((224, 224), PIL.Image.BILINEAR) crop_resized_array_img = image.img_to_array(crop_resized_img) crop_resized_array_preproc_img = preprocess_input( crop_resized_array_img) X_counting[i, ] = crop_resized_array_preproc_img dmap = labels_temp[i] crop_dmap = dmap[random_y1:random_y1 + random_size, random_x1:random_x1 + random_size] # ADB: I implemented a new resizing function that is used here for density maps. y_tmp[i] = downsample(crop_dmap, (14, 14)) y_counting[i] = np.resize(y_tmp[i], (14, 14, 1)) #### ALGORITHM TO GENERATE RANKED DATASETS #### # number of patches to generate k = 5 # scale factor s = 0.75 # anchor region r = 8 for i, image_path in enumerate(list_ranking_imgs_temp): img = image.load_img(image_path) width, height = img.size # select anchor region: crop a patch 1/r of original image centered in the same point and with same aspect ratio anchor_region_width = width * math.sqrt(1 / r) anchor_region_height = height * math.sqrt(1 / r) center_x = width / 2 center_y = height / 2 x1 = int(round(center_x - (anchor_region_width / 2))) y1 = int(round(center_y - (anchor_region_height / 2))) x2 = int(round(center_x + (anchor_region_width / 2))) y2 = int(round(center_y + (anchor_region_height / 2))) # STEP 1: choose an anchor point ramdomly from the anchor region anchor_point_x = random.uniform(x1, x2) anchor_point_y = random.uniform(y1, y2) # STEP 2: find the largest square patch centered at the anchor point and contained within the image boundaries anchor_point_offset_x = width - anchor_point_x anchor_point_offset_y = height - anchor_point_y coord_list = [ anchor_point_x, anchor_point_y, anchor_point_offset_x, anchor_point_offset_y ] patch1_half_width = min(coord_list) patch1_x1 = int(round(anchor_point_x - patch1_half_width)) patch1_y1 = int(round(anchor_point_y - patch1_half_width)) patch1_x2 = int(round(anchor_point_x + patch1_half_width)) patch1_y2 = int(round(anchor_point_y + patch1_half_width)) # first_patch crop_patch = img.crop((patch1_x1, patch1_y1, patch1_x2, patch1_y2)) crop_resized_patch = crop_patch.resize((224, 224), PIL.Image.BILINEAR) crop_resized_array_patch = image.img_to_array(crop_resized_patch) crop_resized_array_preproc_patch = preprocess_input( crop_resized_array_patch) X_ranking[i * k, ] = crop_resized_array_preproc_patch patches_list = list() patches_list.append(crop_patch) #STEP 3: Crop k-1 additional square patches, reducing size iteratively by a scale factor s. Keep all patches centered at anchor point for j in range(2, k + 1): patch = patches_list[j - 2] w, h = patch.size crop_width = w * math.sqrt(s) cen_x = w / 2 cen_y = h / 2 xy1 = int(round(cen_x - (crop_width / 2))) xy2 = int(round(cen_x + (crop_width / 2))) crop_subpatch = patch.crop((xy1, xy1, xy2, xy2)) patches_list.append(crop_subpatch) crop_resized_subpatch = crop_subpatch.resize( (224, 224), PIL.Image.BILINEAR) crop_resized_array_subpatch = image.img_to_array( crop_resized_subpatch) crop_resized_array_preproc_subpatch = preprocess_input( crop_resized_array_subpatch) X_ranking[(i * k) + j - 1, ] = crop_resized_array_preproc_subpatch # dummy ranking batch target y_ranking = np.zeros((self.batch_size, 1)) return { 'counting_input': X_counting, 'ranking_input': X_ranking }, { 'counting_output': y_counting, 'ranking_output': y_ranking }
save_plots = True target_size = 100 # download dataset from http://vis-www.cs.umass.edu/lfw/lfw.tgz images = glob.glob(os.path.join('data', 'lfw', '**', '*.jpg')) hi_res_dataset = [] lo_res_dataset = [] ratio = 0.40 low_res_dim = (int(target_size * ratio), int(target_size * ratio)) # for i in tqdm(images[:1000]): for i in tqdm(images): hr_img = image.load_img(i, target_size=(target_size, target_size, 3)) hr_img = image.img_to_array(hr_img) lr_img = smart_resize(hr_img, size=low_res_dim) lr_img = smart_resize(lr_img, size=(target_size, target_size)) hr_img = hr_img / 255. lr_img = lr_img / 255. hi_res_dataset.append(hr_img) lo_res_dataset.append(lr_img) hi_res_dataset = np.array(hi_res_dataset) lo_res_dataset = np.array(lo_res_dataset) # Hyperparameters learning_rate = 0.001 n_epochs = 50 batch_size = 256 validation_split = 0.1
def predictMultipleImages(self, sent_images_array, result_count_per_image=2, input_type="file"): """ 'predictMultipleImages()' function is used to predict more than one image by receiving the following arguments: * input_type , the type of inputs contained in the parsed array. Acceptable values are "file", "array" and "stream" * sent_images_array , an array of image file paths, image numpy array or image file stream * result_count_per_image (optionally) , the number of predictions to be sent per image, which must be whole numbers between 1 and 1000. The default is 2. This function returns an array of dictionaries, with each dictionary containing 2 arrays namely 'prediction_results' and 'prediction_probabilities'. The 'prediction_results' contains possible objects classes arranged in descending of their percentage probabilities. The 'prediction_probabilities' contains the percentage probability of each object class. The position of each object class in the 'prediction_results' array corresponds with the positions of the percentage possibilities in the 'prediction_probabilities' array. :param input_type: :param sent_images_array: :param result_count_per_image: :return output_array: """ output_array = [] for image_input in sent_images_array: prediction_results = [] prediction_probabilities = [] if (self.__modelLoaded == False): raise ValueError("You must call the loadModel() function before making predictions.") else: if (self.__modelType == "squeezenet"): from .imagenet_utils import preprocess_input, decode_predictions if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=(self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") model = self.__model_collection[0] prediction = model.predict(image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image)) for results in predictiondata: countdown = 0 for result in results: countdown += 1 prediction_results.append(str(result[1])) prediction_probabilities.append(str(result[2] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "resnet"): model = self.__model_collection[0] from .imagenet_utils import preprocess_input, decode_predictions if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=(self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image)) for results in predictiondata: countdown = 0 for result in results: countdown += 1 prediction_results.append(str(result[1])) prediction_probabilities.append(str(result[2] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "densenet"): model = self.__model_collection[0] from .DenseNet.densenet import preprocess_input, decode_predictions from .DenseNet.densenet import DenseNetImageNet121 if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=(self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image)) for results in predictiondata: countdown = 0 for result in results: countdown += 1 prediction_results.append(str(result[1])) prediction_probabilities.append(str(result[2] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) elif (self.__modelType == "inceptionv3"): model = self.__model_collection[0] from imageai.Prediction.InceptionV3.inceptionv3 import InceptionV3, preprocess_input, \ decode_predictions if (input_type == "file"): try: image_to_predict = image.load_img(image_input, target_size=(self.__input_image_size, self.__input_image_size)) image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have set a path to an invalid image file.") elif (input_type == "array"): try: image_input = Image.fromarray(np.uint8(image_input)) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong numpy array for the image") elif (input_type == "stream"): try: image_input = Image.open(image_input) image_input = image_input.resize((self.__input_image_size, self.__input_image_size)) image_input = np.expand_dims(image_input, axis=0) image_to_predict = image_input.copy() image_to_predict = np.asarray(image_to_predict, dtype=np.float64) image_to_predict = preprocess_input(image_to_predict) except: raise ValueError("You have parsed in a wrong stream for the image") prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions(prediction, top=int(result_count_per_image)) for results in predictiondata: countdown = 0 for result in results: countdown += 1 prediction_results.append(str(result[1])) prediction_probabilities.append(str(result[2] * 100)) except: raise ValueError("An error occured! Try again.") each_image_details = {} each_image_details["predictions"] = prediction_results each_image_details["percentage_probabilities"] = prediction_probabilities output_array.append(each_image_details) return output_array
def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size): imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths] img_array = np.array([img_to_array(img) for img in imgs]) return preprocess_input(img_array)
def picture_to_tensor(filename): img = img_to_array(load_img(filename)) # Returns 3D np array. return img.reshape([1, img_rows, img_cols, img_channels])
for idx in class_idx: cam = np.dot(weight_softmax[:, idx], np.transpose(feature_conv.reshape(h*w, nc))) cam = cam.reshape(h, w) cam = cam - np.min(cam) cam_img = cam / np.max(cam) cam_img = np.uint8(255 * cam_img) output_cam.append(cv2.resize(cam_img, size_upsample)) return output_cam response = requests.get(IMG_URL) img_pil = Image.open(io.BytesIO(response.content)) img_pil.save('test.jpg') img_array = img_to_array(load_img('test.jpg', target_size=(HEIGHT, WIDTH))) img_array = preprocess_input(img_array) probs_extractor = K.function([model.input], [model.output]) # This is how we get intermediate layer output in Keras (this returns a callable) features_conv_extractor = K.function([model.input], [fianlconv.output]) classes = {int(key):value for (key, value) in requests.get(LABELS_URL).json().items()} # Getting final layer output probs = probs_extractor([np.expand_dims(img_array, 0)])[0] # Getting output of last conv layer features_blob = features_conv_extractor([np.expand_dims(img_array, 0)])[0]
def img_to_array(raw_img): img_width, img_height = 256, 256 img = image.load_img(raw_img, target_size=(img_width, img_height)) img = image.img_to_array(img) / 255. img = np.expand_dims(img, axis=0) return img
def load_imgs(img_paths, target_size): list_imgs = [img_to_array(load_img(path, target_size=target_size)) for path in img_paths] return np.array(list_imgs)
z = model.predict(np.expand_dims(x, axis=0)) print(z.shape) z = np.squeeze(z) y = np.argmax(z, axis=-1) img_color = image.copy() img_prob = np.zeros([h, w, 3]) print(img_color.shape) img_color[:, :, 0] = y img_color[:, :, 1] = 0 img_color[:, :, 2] = 0 return img_color * 50 image_dir = 'Data/BRATS_20_Val_full_png' image_list = os.listdir(image_dir) image_list.sort() print(f'{len(image_list)} frames found') for i in tqdm(range(len(image_list))): test = load_data(f'{image_dir}/{image_list[i]}') test = img_to_array(test) segmap = pipeline(test) fname = f'{image_list[i]}' cv2.imwrite(f'Data/BRATS_20_Validation_mask_results_png/{fname}', cv2.cvtColor(segmap, cv2.COLOR_RGB2BGR))
# grab the list of images in our dataset directory, then initialize # the list of data (i.e., images) and class images print("[INFO] loading images...") imagePaths = list(paths.list_images(args["dataset"])) data = [] labels = [] # loop over the image paths for imagePath in imagePaths: # extract the class label from the filename label = imagePath.split(os.path.sep)[-2] # load the input image (224x224) and preprocess it image = load_img(imagePath, target_size=(224, 224)) image = img_to_array(image) image = preprocess_input(image) # update the data and labels lists, respectively data.append(image) labels.append(label) # convert the data and labels to NumPy arrays data = np.array(data, dtype="float32") labels = np.array(labels) # perform one-hot encoding on the labels lb = LabelBinarizer() labels = lb.fit_transform(labels) labels = to_categorical(labels)
def load_image(image_path, size): return img_to_array(load_img(image_path, target_size=(size, size))) / 255.
def predictImage(self, image_path, result_count=5): """ 'predictImage()' function is used to predict a given image by receiving the following arguemants: * image_path , file path to the image * result_count (optionally) , the number of predictions to be sent which must be whole numbers between 1 and 1000. The default is 5. This function returns 2 arrays namely 'result_prediction' and 'prediction_probabilities'. The 'result_prediction' contains possible objects classes arranged in descending of their percentage probabilities. The 'prediction_probabilities' contains the percentage probability of each object class. The position of each object class in the 'result_prediction' array corresponds with the positions of the percentage possibilities in the 'prediction_probabilities' array. :param image_path: :param result_count: :return result_prediction, prediction_probabilities: """ result_prediction = [] prediction_probabilities = [] if (self.__modelLoaded == False): raise "You must call the loadModel() function before making predictions." else: if (self.__modelType == "squeezenet"): try: from .SqueezeNet.imagenet_utils import preprocess_input, decode_predictions img = image.load_img(image_path, target_size=(227, 227)) img = image.img_to_array(img, data_format="channels_last") img = np.expand_dims(img, axis=0) img = preprocess_input(img, data_format="channels_last") except: raise "You have set a path to an invalid image file." model = self.__model_collection[0] prediction = model.predict(img, steps=1) try: predictiondata = decode_predictions( prediction, top=int(result_count), index_file_path=self.jsonPath) for results in predictiondata: countdown = 0 for result in results: countdown += 1 result_prediction.append(str(result[1])) prediction_probabilities.append( str(result[2] * 100)) except: raise "You have set a wrong path to the JSON file" return result_prediction, prediction_probabilities elif (self.__modelType == "resnet"): model = self.__model_collection[0] try: from .ResNet.imagenet_utils import preprocess_input, decode_predictions target_image = image.load_img(image_path, grayscale=False, target_size=(224, 224)) target_image = image.img_to_array( target_image, data_format="channels_last") target_image = np.expand_dims(target_image, axis=0) target_image = preprocess_input( target_image, data_format="channels_last") except: raise "You have set a path to an invalid image file." prediction = model.predict(x=target_image, steps=1) try: predictiondata = decode_predictions( prediction, top=int(result_count), index_file_path=self.jsonPath) for results in predictiondata: countdown = 0 for result in results: countdown += 1 result_prediction.append(str(result[1])) prediction_probabilities.append( str(result[2] * 100)) except: raise "You have set a wrong path to the JSON file" return result_prediction, prediction_probabilities elif (self.__modelType == "densenet"): model = self.__model_collection[0] try: from .DenseNet.densenet import DenseNetImageNet121, preprocess_input, decode_predictions from .DenseNet.densenet import DenseNetImageNet121 image_to_predict = image.load_img(image_path, grayscale=False, target_size=(224, 224)) image_to_predict = image.img_to_array( image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input( image_to_predict, data_format="channels_last") except: raise "You have set a path to an invalid image file." prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions( prediction, top=int(result_count), index_file_path=self.jsonPath) for results in predictiondata: countdown = 0 for result in results: countdown += 1 result_prediction.append(str(result[1])) prediction_probabilities.append( str(result[2] * 100)) except: raise "You have set a wrong path to the JSON file" return result_prediction, prediction_probabilities elif (self.__modelType == "inceptionv3"): model = self.__model_collection[0] try: from imageai.Prediction.InceptionV3.inceptionv3 import InceptionV3 from imageai.Prediction.InceptionV3.inceptionv3 import preprocess_input, decode_predictions image_to_predict = image.load_img(image_path, grayscale=False, target_size=(299, 299)) image_to_predict = image.img_to_array( image_to_predict, data_format="channels_last") image_to_predict = np.expand_dims(image_to_predict, axis=0) image_to_predict = preprocess_input(image_to_predict) except: raise "You have set a path to an invalid image file." prediction = model.predict(x=image_to_predict, steps=1) try: predictiondata = decode_predictions( prediction, top=int(result_count), index_file_path=self.jsonPath) for results in predictiondata: countdown = 0 for result in results: countdown += 1 result_prediction.append(str(result[1])) prediction_probabilities.append( str(result[2] * 100)) except: raise "You have set a wrong path to the JSON file" return result_prediction, prediction_probabilities
class KagglePlanetSequence(tf.keras.utils.Sequence): """ Custom Sequence object to train a model on out-of-memory datasets. """ def __init__(self, df, data_path, im_size, batch_size, mode='train'): """ df: pandas dataframe that contains columns with image names and labels data_path: path that contains the training images im_size: image size mode: when in training mode, data will be shuffled between epochs """ self.df = df self.batch_size = batch_size self.im_size = im_size self.mode = mode # Take labels and a list of image locations in memory self.wlabels = self.df['weather_labels'].apply(lambda x: ast.literal_eval(x)).tolist() self.glabels = self.df['ground_labels'].apply(lambda x: ast.literal_eval(x)).tolist() self.image_list = self.df['image_name'].apply(lambda x: os.path.join(data_path, x + '.jpg')).tolist() def __len__(self): return int(math.ceil(len(self.df) / float(self.batch_size))) def on_epoch_end(self): # Shuffles indexes after each epoch self.indexes = range(len(self.image_list)) if self.mode == 'train': self.indexes = random.sample(self.indexes, k=len(self.indexes)) def get_batch_labels(self, idx): # Fetch a batch of labels return [self.wlabels[idx * self.batch_size: (idx + 1) * self.batch_size], self.glabels[idx * self.batch_size: (idx + 1) * self.batch_size]] def get_batch_features(self, idx): # Fetch a batch of images batch_images = self.image_list[idx * self.batch_size: (1 + idx) * self.batch_size] return np.array([load_image(im, self.im_size) for im in batch_images]) def __getitem__(self, idx): batch_x = self.get_batch_features(idx) batch_y = self.get_batch_labels(idx) return batch_x, batch_y seq = KagglePlanetSequence(df_train, './train-jpg/', im_size=IM_SIZE, batch_size=32) # In[ ]: callbacks = [ tf.keras.callbacks.ModelCheckpoint('./model.h5', verbose=1) ] model.fit_generator(generator=seq, verbose=1, epochs=1, use_multiprocessing=False, workers=1, callbacks=callbacks) # In[ ]: another_model = tf.keras.models.load_model('./model.h5') another_model.fit_generator(generator=seq, verbose=1, epochs=1) # In[ ]: test_seq = KagglePlanetSequence(df_train, './train-jpg/', im_size=IM_SIZE, batch_size=32, mode='test') # test mode disables shuffling predictions = model.predict_generator(generator=test_seq, verbose=1) # We get a list of two prediction arrays, for weather and for label # In[ ]: len(predictions[1]) == len(df_train) # Total number of images in dataset # In[ ]: # Serialize images, together with labels, to TF records def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) tf_records_filename = './/KagglePlanetTFRecord_{}'.format(IM_SIZE) writer = tf.python_io.TFRecordWriter(tf_records_filename) # List of image paths, np array of labels im_list = [os.path.join('./train-jpg/', v + '.jpg') for v in df_train['image_name'].tolist()] w_labels_arr = np.array([ast.literal_eval(l) for l in df_train['weather_labels']]) g_labels_arr = np.array([ast.literal_eval(l) for l in df_train['ground_labels']]) for i in range(len(df_train)): w_labels = w_labels_arr[i].astype(np.float32) g_labels = g_labels_arr[i].astype(np.float32) im = np.array(img_to_array(load_img(im_list[i], target_size=(IM_SIZE, IM_SIZE))) / 255.) w_raw = w_labels.tostring() g_raw = g_labels.tostring() im_raw = im.tostring() example = tf.train.Example(features=tf.train.Features(feature={'image': _bytes_feature(im_raw), 'weather_labels': _bytes_feature(w_raw), 'ground_labels': _bytes_feature(g_raw)})) writer.write(example.SerializeToString()) writer.close() # In[ ]: from tensorflow import FixedLenFeature featdef = { 'image': FixedLenFeature(shape=[], dtype=tf.string), 'weather_labels': FixedLenFeature(shape=[], dtype=tf.string), 'ground_labels': FixedLenFeature(shape=[], dtype=tf.string) } # In[ ]: def _parse_record(example_proto, clip=False): ex = tf.parse_single_example(example_proto, featdef) im = tf.decode_raw(ex['image'], tf.float32) im = tf.reshape(im, (IM_SIZE, IM_SIZE, 3)) weather = tf.decode_raw(ex['weather_labels'], tf.float32) ground = tf.decode_raw(ex['ground_labels'], tf.float32) return im, (weather, ground) # Construct a dataset iterator batch_size = 32 ds_train = tf.data.TFRecordDataset('./KagglePlanetTFRecord_{}'.format(IM_SIZE)).map(_parse_record) ds_train = ds_train.repeat().shuffle(1000).batch(batch_size) # In[ ]: model = tf.keras.Model(inputs=image_input, outputs=[weather_output, ground_output]) model.compile(optimizer='adam', loss={'weather': 'categorical_crossentropy', 'ground': 'binary_crossentropy'}) history = model.fit(ds_train, steps_per_epoch=100, # let's just take some steps epochs=1) # In[ ]: import shutil tf.keras.backend.clear_session() tf.keras.backend.set_learning_phase(0) model = tf.keras.models.load_model('./model.h5') if os.path.exists('./PlanetModel/1'): shutil.rmtree('./PlanetModel/1') export_path = './PlanetModel/1' # Fetch the Keras session and save the model with tf.keras.backend.get_session() as sess: tf.saved_model.simple_save( sess, export_path, inputs={'input_image': model.input}, outputs={t.name:t for t in model.outputs})
net = load_model( '/home/vidooly/ml/projects/user/vikas/kiss_classifier/model/best_path_merge_foc2.hdf5', custom_objects=custom_object) j = 0 for root, dirs, files in os.walk(inp_path): for file in files: f = os.path.join(root, file) sub_fol = f.split('/')[-2] # print(f) # break try: img = image.load_img(f, target_size=(224, 224)) if img is None: continue x = image.img_to_array(img) x = preprocess_input(x) x = np.expand_dims(x, axis=0) pred = net.predict(x)[0] # img,pred=get_image(f) adult_score = round(float(pred[1]), 3) out_fn = str(adult_score) + ',' + file # img.save('out_dir+sub_fol+'/'+str(math.floor(adult_score*10))+'/'+out_fn') shutil.copy( f, out_dir + sub_fol + '/' + str(math.floor(adult_score * 10)) + '/' + out_fn) except Exception as e: print(e) pass j += 1 # print("Complete: ", j)
def extract_one_image_feature(image): numpy_image = img_to_array(image) image_batch = np.expand_dims(numpy_image, axis=0) print('image batch size', image_batch.shape) preprocessed_image = preprocess_input(image_batch.copy()) return preprocessed_image
def process_image(image): image = kp_image.img_to_array(image) image = np.expand_dims(image, axis=0) return tf.keras.applications.vgg19.preprocess_input(image)
def preprocess(image_path): image = load_img(image_path) image = img_to_array(image) image = np.expand_dims(image, axis=0) return preprocess_input(image)
def load_imgs(img_paths, target_size): list_imgs = [img_to_array(load_img(path, target_size=target_size)) for path in img_paths] return np.array(list_imgs)
while True: counter += 1 # grab the current frame (grabbed, image) = vs.read() # if we are viewing a video and we did not grab a frame, then we # have reached the end of the video if input_video and not grabbed: break if not input_video: image = cv2.flip(image, 1) roi = cv2.resize(image, (TARGET_WIDTH, TARGET_HEIGHT)) roi = roi.astype("float") / 255.0 roi = img_to_array(roi) roi = np.expand_dims(roi, axis=0) # determine facial expression (fall, normal) = model.predict(roi)[0] label = "Fall (%.2f)" % (fall) if fall > normal else "Normal (%.2f)" % ( normal) # display the label and bounding box rectangle on the output frame cv2.putText(image, label, (image.shape[1] - 150, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) if fall > normal: if fall_timing == 0: # just start timing fall_timing = 1 fall_start_time = time.time()
def load_and_process_image(path): image_array = image.img_to_array(image.load_img(path)) return kr.applications.vgg19.preprocess_input(image_array)
my_new_model = model_from_json(model_arc_data) my_new_model.load_weights('my_model_weights.h5') my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) probs = np.zeros((len(lis), 30)) jj = 0 pic_in = open('animal_dict', 'rb') anim_dic = pickle.load(pic_in) pic_in.close() lims = sorted(anim_dic.keys()) for i in liss: imgs = load_img(i, target_size=(224, 224)) img_arr = np.array([img_to_array(imgs)]) test_data = preprocess_input(img_arr) preds = my_new_model.predict(test_data) probs[jj] = preds[0] # preds=my_new_model.predict_classes(test_data) # print(lims[preds[0]],preds[0]) jj += 1 print(jj) pic_in = open('probs', 'wb') pickle.dump(probs, pic_in) pic_in.close() lis = lis.reshape(len(lis), 1) Final_set = np.concatenate((lis, probs), axis=1) pic_in = open('animal_dict', 'rb')