def load_images(data_type): dataset = [] path = "dataset/" + data_type + "/images" categories = os.listdir(path) for category in categories: current_path = path + "/" + category class_num = categories.index(category) model = DenseNet169(include_top=False, input_shape=(224, 224, 3)) for image_path in tqdm(os.listdir(current_path)): img = image.load_img(current_path + "/" + image_path, target_size=(224, 224)) if data_type == "train": for i in augment(img): img_data = image.img_to_array(i) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) features = model.predict(img_data) dataset.append([features, class_num]) else: img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) features = model.predict(img_data) dataset.append([features, class_num]) print("Loaded " + data_type) if data_type == "train": random.shuffle(dataset) return dataset
def load_image(mode, path, target_size): w, h = 32, 32 x = [ 0, 0, 0, 0, 0, 0, 0, 32, 32, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 64, 96, 96, 96, 96, 96, 96, 96, 128, 128, 128, 128, 128, 128, 128, 160, 160, 160, 160, 160, 160, 160, 192, 192, 192, 192, 192, 192, 192 ] y = [ 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192, 0, 32, 64, 96, 128, 160, 192 ] try: image = load_img(path, target_size=target_size) # convert the image pixels to a numpy array image = img_to_array(image) except: print(path) image = cv2.imread(path) image = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA) image = Image.fromarray(image) image = img_to_array(image) #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if mode == 1 or mode == 2: print('preprocess_done') image = preprocess_input(image) image = np.expand_dims(image, axis=0) if mode == 3: image = preprocess_input(image) image = [image[y[i]:y[i] + h, x[i]:x[i] + w] for i in range(49)] return image
def predict(): message = request.get_json(force=True) decoded_image = base64.b64decode(message['image']) pil_img = Image.open(io.BytesIO(decoded_image)) processed_image = preprocess_image(pil_img, target_size=(224, 224)) interpreter = tf.lite.Interpreter(model_path="./model.tflite") interpreter.allocate_tensors() x = preprocess_input(processed_image) input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() interpreter.set_tensor(input_details[0]['index'], x) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) label_array = [ 'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika', 'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato', 'turnip', 'watermelo' ] max = 0 label_loc = 0 for i in range(0, len(output_data[0])): if (output_data[0][i] > max): max = output_data[0][i] label_loc = i prediction = label_array[label_loc] response = {'prediction': prediction} return jsonify(response)
def main(): parser = argparse.ArgumentParser( description='training script for a paraphrase classifier') parser.add_argument('--type', '-t', type=int, default=0) args = parser.parse_args() os.environ["CUDA_VISIBLE_DEVICES"] = str(args.type % 4) print('DenseNet-121 for finetune', args.type, 'layers') print('Using GPU', args.type) pretrained = get_pretrained_model(args.type) img_dir = '../data/flickr30k-images/' emb_dir = '../data/old_VGP/densenet_visual_embedding/' + str( args.type) + '/' if not os.path.exists(emb_dir): os.makedirs(emb_dir) list_file = os.listdir(img_dir) img_path_list = [img_dir + file_name for file_name in list_file] i = 0 with progressbar.ProgressBar(max_value=len(img_path_list)) as bar: for img_path in img_path_list: 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 = pretrained.predict(x) npy_file_name = emb_dir + img_path[len(img_dir):-4] np.save(npy_file_name, np.squeeze(preds)) i += 1 bar.update(i)
def model_predict(img, model): img = img.resize(img_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return preds
def load_dataset_et_process(dataset_path): paths_images = paths.list_images(dataset_path) data = [] labels = [] # Loops over the whole dataset for image_path in paths_images: # Extracts the label from each class label = image_path.split(os.path.sep)[1] labels.append(label) # To load the image resizes it into 224x224 img = load_img(image_path, target_size=(224, 224)) # Converts a PIL to a Numpy array img_array = img_to_array(img) # Preprocesses the input image image_array = preprocess_input(img_array) data.append(image_array) data = np.array(data, dtype='float32') labels = np.array(labels) print(data.shape) print(labels.shape) return data, labels
def load_image(img_size, path='', url=''): def resize_to_square(im, img_size): old_size = im.shape[:2] # old_size is in (height, width) format ratio = float(img_size) / max(old_size) new_size = tuple([int(x * ratio) for x in old_size]) # new_size should be in (width, height) format im = cv2.resize(im, (new_size[1], new_size[0])) delta_w = img_size - new_size[1] delta_h = img_size - new_size[0] top, bottom = delta_h // 2, delta_h - (delta_h // 2) left, right = delta_w // 2, delta_w - (delta_w // 2) color = [0, 0, 0] new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) return new_im if url == '': image = cv2.imread(path) elif path == '': # download the image, convert it to a NumPy array, and then read # it into OpenCV format resp = urllib.request.urlopen(url) image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) new_image = resize_to_square(image, img_size) new_image = preprocess_input(new_image) return new_image
def build_model(classes=2): inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) x = preprocess_input(inputs) x = DenseNet201(weights=None, classes=classes)(x) model = Model(inputs=inputs, outputs=x) model.compile(loss='categorical_crossentropy', metrics=['accuracy']) return model
def get_preprocessed_input(img): img = get_img_crop(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) # cv2.imshow("preprocessed:", img[0, :, :, :]) return img
def extract_transfer_learning_features(img_path): model = DenseNet121(weights='imagenet', include_top=False) img = image.load_img(img_path, target_size=(331, 331)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = model.predict(x).flatten() return features
def detect_predict_mask(frame, face_net, mask_net): # Acquire the dimensions of the frame and construct the blob object print('frame shape : {}'.format(frame.shape)) high, weight = frame.shape[:2] blob = cv2.dnn.blobFromImage(image=frame, scalefactor=1.0, size=(300, 300), mean=(104.0, 177.0, 123.0)) # Pass the blob through the network and obtain the face detections face_net.setInput(blob) detections = face_net.forward() print('detections shape : ', detections.shape) print('detections shape[2] : ', detections.shape[2]) print(detections) # Initialize the list of faces object, their corresponding locations, and the one of predictions from the face mask faces = [] locations = [] predictions = [] # Loop over the detections for i in range(0, detections.shape[2]): # Extract the confidence associated with the detection confidence = detections[0, 0, i, 2] # Cut off weak detections by guaranteeing confidence is greater than the minimum confidence if confidence > args['confidence']: # Compute the (x,y) -- coordinates of the bounding box for each object bounding_box_object = detections[0, 0, i, 3:7] * np.array( [weight, high, weight, high]) start_x, start_y, end_x, end_y = bounding_box_object.astype('int') # Guaranteeing the bounding boxes fall within the frame (start_x, start_y) = (max(0, start_x - 7), max(0, start_y - 7)) (end_x, end_y) = (min(weight, end_x + 7), min(high, end_y + 7)) # Extract the ROI (Region of interest) face = frame[start_y:end_y, start_x:end_x] # Convert it from BGR to RGB face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) # Resize it to 224*224 face = cv2.resize(face, (224, 224)) # Transform face into numpy array face = img_to_array(face) # processing it face = preprocess_input(face) print(face) # Add the face and the location of the bounding box to respective list faces.append(face) locations.append((start_x, start_y, end_x, end_y)) # To make the prediction is only in the case of at least existing one face. if len(faces) > 0: faces = np.array(faces, dtype='float32') predictions = mask_net.predict(faces, batch_size=32) return locations, predictions
def get_image(list_file_name): list_file_name = list_file_name.numpy() n = len(list_file_name) img = np.zeros((n, 224, 224, 3)) for i in range(n): file_name = list_file_name[i].decode('ascii')[:-4] + '.jpg' temp = image.load_img(img_dir + file_name, target_size=(224, 224)) img[i] = image.img_to_array(temp) img = preprocess_input(img) return img
def predict(self, **data): if self.model is None: self.model = load_model(self.model_path) # 获取需要预测的图像数据, predict_data 方法默认会去调用 processor.py 中的 input_x 方法 x_data = self.dataset.predict_data(**data) x_data = preprocess_input(x_data, ) predict = self.model.predict(x_data) # 将预测数据转换成对应标签 to_categorys 会去调用 processor.py 中的 output_y 方法 prediction = self.dataset.to_categorys(predict) return prediction
def _extract_predictions(self, img: Image) -> list: img = img.resize((self.IMG_HEIGHT, self.IMG_HEIGHT)) img = img.convert('RGB') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x).tolist() response = requests.post( f'{config.TENSORFLOW_SERVING_MODEL_ADDRESS}:predict', json={'instances': x} ) json_response = response.json() return json_response.get('predictions')
def load_batch(batch_of_files, is_training=False): batch_images = [] batch_labels = [] for filename in batch_of_files: img = pil.open(os.path.join('./images_all/images', filename)) img = img.convert('RGB') img = img.resize((256, 256), pil.NEAREST) if is_training and np.random.randint(2): img = PIL.ImageOps.mirror(img) batch_images.append(np.asarray(img)) batch_labels.append(labels[filename]) return preprocess_input(np.float32(np.asarray(batch_images))), np.asarray(batch_labels)
def prepare_image(image, target): # convert image if not RGB if image.mode != 'RGB': image = image.convert('RGB') # resize input image and reprocess it image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = preprocess_input(image) return image
def f(X): sum = X[0] * encodings[0] for i in range(1, len(X)): sum += X[i] * encodings[i] decoded_image = (decode_image(sum) * 255).astype(np.uint8) decoded_image = Image.fromarray(decoded_image) decoded_image = decoded_image.resize((224, 224)) decoded_image = np.expand_dims(decoded_image, axis=0) decoded_image = preprocess_input(decoded_image) features = cnn.predict(decoded_image) answer = image_classifier.predict(features)[0][category] return -answer
def pre_process_built_cnn(x, type_cnn): """ Method that pre-processes images based on the specified built convolutional neural network. Some of the supported CNNs include: VGG16, InceptionV3, DenseNet, ResNet50, and InceptionResNetV2 :param x: (np_array) matrix with all image data with shape [n_samples, img_width, img_height] :param type_cnn: (str) type of CNN to pre-process the data. Currently supported strings are: 'inceptionv3', 'densenet', 'inception_resnet_v2', 'resnet50', and 'vgg16' :return: (np_array) pre-processed data. """ valid_cnn_types = [ 'inceptionv3', 'densenet', 'inception_resnet_v2', 'resnet50', 'vgg16' ] if type_cnn not in valid_cnn_types: raise ValueError('You entered an invalid type of CNN: {}. ' 'Valid CNN type arguments include: {}'.format( type_cnn, valid_cnn_types)) if len(x.shape) < 3 or len(x.shape) > 4: raise DimensionalityError( 'Cannot pre-process image data with shape {} provided for the' 'training data "x".\n' 'Valid shapes are: (n_samples, img_width, img_height)' ' and (n_samples, img_width, img_height, n_channels)'.format( x.shape)) # Transform gray scale data to RGB if len(x.shape) == 3: try: x = np.array([ np.array(Image.fromarray(x_i).convert('RGB')) for x_i in x ]) except TypeError: x = np.array([ np.array(Image.fromarray(np.uint8(x_i)).convert('RGB')) for x_i in x ]) if type_cnn == 'vgg16': x = vgg16.preprocess_input(x) elif type_cnn == 'resnet50': x = resnet50.preprocess_input(x) elif type_cnn == 'inception_resnet_v2': x = inception_resnet_v2.preprocess_input(x) elif type_cnn == 'densenet': x = densenet.preprocess_input(x) elif type_cnn == 'inceptionv3': x = inception_v3.preprocess_input(x) return x
def crop_generator(batches, target_size=224, scale=(0.4, 1.0), occlusion=False): """Take as input a Keras ImageGen (Iterator) and generate random crops from the image batches generated by the original iterator. """ while True: batch_x, batch_y = next(batches) batch_crops = np.zeros((batch_x.shape[0], target_size[0], target_size[1], 3)) for i in range(batch_x.shape[0]): # Random contrast equalization # if random.random() > 0.5: # batch_crops[i] = histogram_equalize(batch_crops[i]) batch_crops[i] = RandomResizedCrop(batch_x[i], size=target_size, scale=scale, occlusion=occlusion) batch_crops = preprocess_input(batch_crops) yield (batch_crops, batch_y)
def load_image(path, target_size): try: image = load_img(path, target_size=target_size) # convert the image pixels to a numpy array image = img_to_array(image) except: print(path) image = cv2.imread(path) image = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA) image = Image.fromarray(image) image = img_to_array(image) image = preprocess_input(image) return image
def infer(model, detector, img_path, input_shape, is_debug=True): img = image.load_img(img_path) img = image.img_to_array(img) new_img = preprocess(detector, img, input_shape) if type(new_img) == type(None): return None if is_debug: plt.figure() plt.imshow(new_img.astype('uint8')) plt.show() x = np.expand_dims(new_img, axis=0) x = preprocess_input(x) pred = model.predict(x) return pred
def predict(model, img, target_size): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (w,h) tuple Returns: list of predicted labels and their probabilities """ if img.size != target_size: img = img.resize(target_size) x = tf.keras.preprocessing.image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return preds
def evaluate(model, dataset, append_submission, dataset_root): """ Running evaluation on test set, appending results to a submission. """ with open(os.path.join(dataset_root, dataset + '.json'), 'r') as f: image_list = json.load(f) print('Running evaluation on {} set...'.format(dataset)) for img in image_list: img_path = os.path.join(dataset_root, 'images', dataset, img['filename']) pil_img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(pil_img) x = preprocess_input(x) x = np.expand_dims(x, 0) output = model.predict(x) append_submission(img['filename'], output[0, :4], output[0, 4:])
def make_densenet(inputshape): base_model = DenseNet121( input_tensor=Input(inputshape), include_top=False,) base_model.trainable= False inputs = Input(inputshape) x = densenet.preprocess_input(inputs) x = base_model(x, training=False) x = GlobalAveragePooling2D()(x) x = Dropout(0.3)(x) outputs = Dense(1, activation='sigmoid')(x) model = Model(inputs, outputs) return model
def process(frame): """ process pic for capture script """ # resize the image img = cv2.resize(frame, (224, 224)) #img = img.resize((224, 224)) # convert color from BGR to RGB img = cv2.cvtColor( np.float32(img), cv2.COLOR_BGR2RGB) #how to change to black/white for MNist dataset? # possibly do some preprocessing e.g. converting it to range [0,1] # reshape it into shape (1, target_width, target_height, 3) img = preprocess_input(img) #change to densenet!!! img = img.reshape(1, 224, 224, 3) return img
def combine_images(variables, encodings): cnn = DenseNet169(include_top=False, input_shape=(224, 224, 3)) image_classifier = load_model('image_model.h5') sum = variables[0] * encodings[0] for i in range(1, len(variables)): sum += variables[i] * encodings[i] decoded_image = (decode_image(sum) * 255).astype(np.uint8) decoded_image = Image.fromarray(decoded_image) decoded_image = decoded_image.resize((224, 224)) plt.imshow(decoded_image) plt.show() decoded_image = np.expand_dims(decoded_image, axis=0) decoded_image = preprocess_input(decoded_image) features = cnn.predict(decoded_image) prediction = image_classifier.predict(features) print("Prediction:", prediction)
def load_image(path, target_size): try: image = load_img(path, target_size=target_size) # convert the image pixels to a numpy array image = img_to_array(image) except: print(path) image = cv2.imread(path) image = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA) image = Image.fromarray(image) image = img_to_array(image) #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = preprocess_input(image) #image = np.expand_dims(image,axis=0) #print(image.shape) #image = np.resize(image,(224,224,3)) #image = image.astype("float32")/255.0 #image = np.expand_dims(image,axis=0) return image
def predict_from_cv_img(self, img): crop_box = [0, 100, 640, 190] resize_scale = 3 img_size = [int(x/resize_scale) for x in crop_box[2:]] x, y, w, h = crop_box img = img[y:y+h, x:x+w, :] img = cv2.resize(img, tuple(img_size)) img = np.expand_dims(img, axis=0) img = preprocess_input(img) with self.graph.as_default(): out = self.model.predict(img) # print("network out: ", out) bin_num = np.argmax(out) num_bins = 5 bin_size = 2./num_bins cmd_out = bin_num * bin_size + bin_size/2. - 1. # print("cmd out: ", cmd_out) return cmd_out
def get_and_preprocess_img(img_path, img_size, crop_box=[0, 100, 480, 190]): ''' Load and preprocess an image file Parameters: img_path: file path img_size: width and height of image crop_box: [x, y, width, height] of crop ''' img = cv2.imread(img_path) x, y, w, h = crop_box img = img[y:y + h, x:x + w, :] img = cv2.resize(img, tuple(img_size)) img = np.expand_dims(img, axis=0) # add gaussian noise to image something like this # sigma = 1. # gauss = np.random.normal(0., sigma, x.shape) # x = x + gauss # x = np.clip(x, 0., 255.) img = preprocess_input(img) return img
def extract_feature(split): model = visual_model() img_dir = '../data/flickr30k-images/' emb_dir = '../data/non_fine-tuned/densenet_visual_embedding/' + split + '/' # if os.path.isdir(emb_dir)==False: # os.mkdir(emb_dir[-1]) list_id = prepair_data(split) with progressbar.ProgressBar(max_value=len(list_id)) as bar: for i in range(len(list_id)): file_name, rois = extract_info(list_id[i]) img_path = img_dir + file_name + '.jpg' 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) r = np.zeros((1, 4)) r[0] = rois Xvis = model.predict([x, r]) npy_file_name = emb_dir + list_id[i] np.save(npy_file_name, np.squeeze(Xvis)) bar.update(i)