def load_predict_data(image: Union[str, Type[Image]]) -> np.ndarray: ''' Load an image for prediction :param images: a path to image relative to src/ or a PIL.Image.Image object :returns: np.ndarray representing the image ''' if type(image) == str: return preprocess_input(img_to_array(load_img(image, target_size=(150,150)))) elif issubclass(image.__class__, Image): return preprocess_input(img_to_array(image)) else: raise ValueError(f'image argument of type {type(image)} is unacceptable')
def get_model(backbone_name="xception"): if backbone_name == "xception": backbone = Xception(weights='imagenet', input_shape=(384, 384, 3), include_top=False, classes=6) tail_prev = backbone.get_layer('block13_pool').output inputs = backbone.input x = Lambda(lambda image: preprocess_input(image))(inputs) tail = backbone.output output = FinalModel(x, tail_prev, tail, backbone_name) elif backbone_name == "resnet18" or backbone_name == "resnet50": if backbone_name == "resnet18": ResNet18, _ = Classifiers.get('resnet18') backbone = ResNet18(weights='imagenet', input_tensor=Input((384, 384, 3)), include_top=False, classes=6) tail_prev = backbone.get_layer('stage4_unit2_relu2').output elif backbone_name == "resnet50": backbone = ResNet50(weights='imagenet', input_shape=(384, 384, 3), include_top=False, classes=6) tail_prev = backbone.get_layer('conv5_block3_2_relu').output inputs = backbone.input x = Lambda(lambda image: preprocess_input(image))(inputs) tail = backbone.output output = FinalModel(x, tail_prev, tail, backbone_name) if backbone_name == "efficientnetb3": backbone = EfficientNetB3(weights='imagenet', input_shape=(384, 384, 3), include_top=False, classes=6) tail_prev = backbone.get_layer('block7b_project_conv').output inputs = backbone.input x = Lambda(lambda image: preprocess_input(image))(inputs) tail = backbone.output output = FinalModel(x, tail_prev, tail, backbone_name) x = Conv2D( filters=6, kernel_size=(3, 3), padding='same', use_bias=True, kernel_initializer='glorot_uniform', name='final_conv', )(output) x = Activation("softmax", name="softmax")(x) return Model(inputs, x)
def pp(): file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename2 = random.choices(string.ascii_uppercase + string.digits, k=13) filename2 = ''.join(filename2) actual_file_path = os.path.join(upP, filename2) file.save(os.path.join(upP, filename2)) img_path = os.path.join(upP, filename2) img = image.load_img(img_path, target_size=(229, 229)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) os.remove(os.path.join(upP, filename2)) predictions = decode_predictions(preds, top=2)[0] arr = [] for i in range(2): arr.append(predictions[i][1]) return ','.join(arr)
def extract_features(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) f = model.predict(x) return f.flatten()
def __getitem__(self, index): # Generate one batch of data # selects indices of data for next batch indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size] # select data and load images labels = self.labels[indexes] labels = tf.keras.utils.to_categorical(labels, num_classes=self.n_class) images = [cv2.imread(self.images_paths[k]) for k in indexes] images = [cv2.resize(img, self.dim[:2], cv2.INTER_AREA) for img in images] # preprocess and augment data if self.augment == True: images = self.augmentor(images) # dice to decide whether to apply mixup, cutmix, or pass dice = np.random.randint(0, 3) if(dice==0 or dice==1): # Choose random data for mixup or cutmix indexes_rn = self.indexes[~np.isin(self.indexes, indexes)] indexes_2 = np.random.choice(indexes_rn, len(indexes)) # select data and load images labels_2 = self.labels[indexes_2] labels_2 = tf.keras.utils.to_categorical(labels_2, num_classes=self.n_class) images_2 = [cv2.imread(self.images_paths[k]) for k in indexes_2] images_2 = [cv2.resize(img, self.dim[:2], cv2.INTER_AREA) for img in images_2] if(dice==0): images, labels = self.mixup(np.array(images), labels, np.array(images_2), labels_2) else: images, labels = self.cutmix(np.array(images), labels, np.array(images_2), labels_2) images = preprocess_input(np.array(images)) return images, labels
def img2vec_xception(imagedir): image_paths = glob.glob(str(imagedir) + '/*.jpg') _IMAGE_NET_TARGET_SIZE = (299, 299) model = xception.Xception(weights='imagenet') layer_name = 'avg_pool' intermediate_layer_model = Model( inputs=model.input, outputs=model.get_layer(layer_name).output) image_vectors = {} global image_path for c, image_path in enumerate(image_paths): print("\r" + str(c + 1) + "/" + str(len(image_paths)), end="") img = image.load_img(image_path, target_size=_IMAGE_NET_TARGET_SIZE) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = xception.preprocess_input(x) intermediate_output = intermediate_layer_model.predict(x) vector = intermediate_output[0] image_vectors[image_path] = vector embeddings = np.stack(list(image_vectors.values())) with open('xception.pkl', 'wb') as f: pickle.dump(embeddings, f)
def build_model(classes=2): inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) x = preprocess_input(inputs) x = Xception(weights=None, classes=classes)(x) model = Model(inputs=inputs, outputs=x) model.compile(loss='categorical_crossentropy', metrics=['accuracy']) return model
def read_images(pet_ids, target_width=224, target_height=224): # list with images and ids images = [] processed_ids = [] # loop for each pet id in the main dataframe for pet_id in tqdm(pet_ids): try: # reading image and putting it into machine format img = plt.imread(pet_id) img = image.smart_resize(img, (target_width, target_height), interpolation='nearest') img = image.img_to_array(img) img = preprocess_input(img) # saving images.append(img) processed_ids.append(pet_id) # do nothing if passes except: pass return np.array(images), np.array(processed_ids)
def preprocess_image(img_path): FIXED_SIZE = (299, 299) cv_img = cv2.imread(img_path) cv_img = cv2.resize(cv_img, FIXED_SIZE, interpolation=cv2.INTER_NEAREST) cv_img = np.expand_dims(cv_img, axis=0) cv_img = preprocess_input(cv_img) return cv_img
def __getitem__(self, index): # Generate one batch of data # selects indices of data for next batch indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size] # select ocr annots annots = self.ocr_annots[indexes] # select data and load images labels = self.labels[indexes] labels = tf.keras.utils.to_categorical(labels, num_classes=self.n_class) images = [cv2.imread(self.images_paths[k]) for k in indexes] images = [ cv2.resize(img, self.dim[:2], cv2.INTER_AREA) for img in images ] # preprocess and apply TTA if (self.choose != 0): images = self.augmentor(images) return ({"img_input": images, "ann_input": annots}, labels) images = preprocess_input(np.array(images)) return images, labels
def path_to_tensor(img_path): img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
def clean_with_encoder(shared_id, batch_images, encoder: Model = None): """ *_This function has been wrapped in a decorator_* see source code of decorator for usage This function runs encoder over image and outputs images with outlier removed, with labels """ if encoder is None: raise ValueError("A vector encoder is required for this operation") # input images needs to be preprocessed # obtain batch embeddings embeddings = encoder.predict_on_batch(preprocess_input(batch_images)) # do clustering on embeddings possible_identities = DBSCAN( eps=DBSCAN_EPS, min_samples=DBSCAN_MIN_SAMP).fit_predict(embeddings) out_images = [] out_image_ids = [] out_animal_ids = [] # if image is selected, add to output collection for i, learned_label in enumerate(possible_identities): if learned_label == -1: # <= learned label -1 means noise or outlier pass else: out_images.append(batch_images[i].reshape((1, -1))) out_image_ids.append(new_short_id()) out_animal_ids.append(_animal_id_suffixed(shared_id, learned_label)) return out_images, out_image_ids, out_animal_ids,
def prediction(self, update, context): """ Prediction method that using pre-trained on ImageNet dataset Xception model for recognizing sent user's picture returns PICTURE that means switch to picture() method you can infinitely send images to bot until you print /cancel or kill bot process by """ id = str(update.message.from_user.id) img = image.load_img('userID_{}_photo.jpg'.format(id), target_size=(299, 299)) update.message.reply_text('[INFO] Preprocessing...') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) update.message.reply_text('[INFO] Recognizing...') model = Xception() preds = model.predict(x) decoded_preds = decode_predictions(preds, top=1)[0][0] update.message.reply_text('Predicted: {} with {:.2f}% accuracy'.format( decoded_preds[1], decoded_preds[2] * 100)) update.message.reply_text( 'Send me another image or /cancel for stop conversation') return self.PICTURE
def build_model(config): """Builds a keras model using Xception network as core and following instructions on the configuration file. Args: config (dict): Configuration dictionary Returns: keras.model: Keras model """ input_shape = config["model"]["input_shape"] + [3] i = Input( input_shape, name="model_input", ) x = preprocess_input(i) core = Xception(input_shape=input_shape, include_top=False, pooling="avg") if config["model"]["freeze_convolutional_layers"]: print("Freezing convolutional layers") core.trainable = False x = core(x) outputs = [] for clf_layer in config["model"]["target_encoder"]: n_classes = len(config["model"]["target_encoder"][clf_layer]) outputs.append( Dense(units=n_classes, activation="softmax", name=f"{clf_layer}_clf")(x)) model = Model(inputs=i, outputs=outputs) return model
def preprocess_img(img_filepath, rot, crop): ''' Preprocesses single image for graphing and for model prediction Parameters: img_filepath (str): filepath where image lives rot (int): rotation angle; use if image needs to be rotated (non-square) crop (bool): set to true if square crop necessary to prevent distortion upon resize Returns: image_for_pic: image for plotting purposes(normal color scheme) image_for_model: image for model prediction (color scheme set by preprocessing function) ''' loaded = load_img(img_filepath) #if crop, crop to square first so image not too distorted from reality if crop and rot: loaded = loaded.rotate(rot) w, h = loaded.size img = loaded.crop((w // 4, w // 4, 3 * w // 4, 3 * w // 4)) img = img.resize((299, 299)) elif rot: loaded = loaded.rotate(rot) img = loaded.resize((299, 299)) else: img = loaded.resize((299, 299)) arr = img_to_array(img) img_for_pic = arr / 255 #color scaling for picture of img img_for_model = preprocess_input(arr) #color scaling for model return img_for_pic, img_for_model
def generateNewTrainingData(splitRatio, dim=(256, 256)): print("Obtaining images.") images, labels, classes = imageUploadUtils.getAllTrainImages('temp', dim=dim) trainx, testx, trainy, testy = train_test_split(images, labels, test_size=1 - splitRatio, stratify=labels) print("Beginning Xception download.") xc = Xception(include_top=False, weights='imagenet', input_shape=trainx[0].shape) trainx = np.array(trainx) testx = np.array(testx) print("Beginning preprocessing for training set.") trainx = preprocess_input(trainx) print("Beginning preprocessing for testing set.") testx = preprocess_input(testx) print("Beginning passthrough of training set through Xception.") trainx = xc.predict(trainx, verbose=1) print("Beginning passthrough of testing set through Xception.") testx = xc.predict(testx, verbose=1) print(f"{len(trainx)} images have been collected for training.") print(f"{len(testx)} images have been collected for testing.") print( f"is data valid? {len(trainx) == len(trainy) and len(testx) == len(testy)}" ) finaldata = (trainx, np.array(trainy), testx, np.array(testy)) if os.path.exists("output"): directoryUtils.rmtree("output") os.makedirs("output") with open(os.path.join('output', 'labels.dat'), 'wb+') as f: pickle.dump(classes, f) return finaldata
def Xception(image_bytes): image_batch = np.expand_dims(image_bytes, axis=0) processed_imgs = xception.preprocess_input(image_batch) xception_features = xception_extractor.predict(processed_imgs) flattened_features = xception_features.flatten() # normalized_features = flattened_features / norm(flattened_features) return flattened_features
def model_predict(self,image_path): img = image.load_img(image_path, target_size=(299,299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = self.model.predict(x) pred=decode_predictions(predictions, top=3) return ','.join([item[1] for item in pred[0]])
def preprocessing_image(image, target): if image.mode != "RGB": image = image.convert("RGB") image = image.resize(target) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = xception.preprocess_input(image) return image
def get_image_features(cls, image_path): """ This method will return the image features. """ image = IMAGE.load_img(image_path, target_size=(224, 224)) image_data = IMAGE.img_to_array(image) image_data = np.expand_dims(image_data, axis=0) image_data = preprocess_input(image_data) return image_data
def generate_from_paths_and_labels(input_paths, labels, batch_size, input_size=(329, 329), aug=False, mixup=True): num_samples = (len(input_paths) // batch_size) * batch_size while 1: perm = np.random.permutation(num_samples) input_paths = input_paths[perm] labels = labels[perm] for i in range(0, num_samples, batch_size): inputs = list( map(lambda x: image.load_img(x, target_size=input_size), input_paths[i:i + batch_size])) inputs = np.array( list(map(lambda x: image.img_to_array(x), inputs))) if aug: # random horizontal flip # axis: 0 row ,1 col, 2 channel inputs = np.array( list( map( lambda x: flip_axis(x, 1) if np.random.random() < 0.5 else x, inputs))) # print(inputs.shape) #random rotation # inputs = np.array(list(map( # lambda x: image.random_rotation(x,30,row_axis=0, col_axis=1, channel_axis=2), # inputs # ))) # #random shift # inputs = np.array(list(map( # lambda x: image.random_shift(x,0.1,0.1), # inputs # ))) #cut out inputs = np.array(list(map(lambda x: eraser(x), inputs))) inputs = preprocess_input(inputs) # print(inputs, labels[i:i+batch_size]) if mixup: y = np.array(labels[i:i + batch_size]) # print y l = np.random.beta(0.2, 0.2, batch_size // 2) X_l = l.reshape(batch_size // 2, 1, 1, 1) y_l = l.reshape(batch_size // 2, 1) X1 = inputs[:batch_size // 2] X2 = inputs[batch_size // 2:] X = X1 * X_l + X2 * (1 - X_l) y1 = y[:batch_size // 2] y2 = y[batch_size // 2:] y_new = (y1 * y_l + y2 * (1 - y_l)) # print y_new yield (X, y_new) else: yield (inputs, labels[i:i + batch_size])
def homeUp(): if request.method == 'POST': # check if the post request has the file part if 'img' not in request.files: flash('No file part') return redirect(request.url) file = request.files['img'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return redirect(request.url) print(filename) image_size = (299, 299) labels = { 0: 'dress', 1: 'hat', 2: 'longsleeve', 3: 'outwear', 4: 'jeans', 5: 'shirt', 6: 'shoes', 7: 'shorts', 8: 'skirt', 9: 't-shirt' } image = (os.listdir("static/img")) img = load_img("static/img/" + image[0], target_size=(image_size)) x = np.array(img) X = np.array([x]) X = preprocess_input(X) pred = model.predict(X) prediction = labels[pred[0].argmax()] folder = UPLOAD_FOLDER for filename in os.listdir(folder): file_path = os.path.join(folder, filename) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print('Failed to delete %s. Reason: %s' % (file_path, e)) return render_template('product_layout.html', prediction=prediction)
def process_img(img_path, required_size): img = tf.keras.preprocessing.image.load_img(img_path, color_mode='rgb', target_size=required_size) img_array = tf.keras.preprocessing.image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) return img_array
def get_img_array(img_path, size): # `img` is a PIL image of the specified size img = load_img(img_path, target_size=size) # `img_arr` is a float32 Numpy array of shape (size_w, size_h, 3) img_arr = img_to_array(img) # add a dimension to transform our array into a "batch" of size (1, size_w, size_h, 3) img_arr = np.expand_dims(img_arr, axis=0) # scale input pixels between -1 and 1 img_arr = preprocess_input(img_arr) return img_arr
def get_predictions(): model = create_model() # Path of directory containing all model checkpoints path = os.path.join(os.getcwd(), 'r3_Xception_checkpoints') # Filename format: model-1-{epoch:02d}-{val_iou:.2f}-.h5 # Finds the checkpoint with lowest val_loss. This checkpoint is loaded to model. max_iou = 0 # arbitrary large number min_filename = '' min_epoch = 0 for file in os.listdir(path): if file.split('-')[1] == '1': temp = file.split('-')[3] epoch = int(file.split('-')[2]) if float(temp) >= max_iou and epoch > min_epoch: max_iou = float(temp) min_filename = file min_epoch = epoch # Load model weights model.load_weights(os.path.join(path, min_filename)) print('Model loaded') test = pd.read_csv(TEST_PATH, index_col='image_name') count = 0 for filename in glob.glob(IMAGES): if count % 1000 == 0: print('Number of images predicted:', count) count += 1 unscaled = cv2.imread(filename) # image_height, image_width, _ = unscaled.shape image_height, image_width = 480, 640 try: image = cv2.resize(unscaled, (IMAGE_SIZE[1], IMAGE_SIZE[0])) feat_scaled = preprocess_input(np.array(image, dtype=np.float32)) region = model.predict(x=np.array([feat_scaled]))[0] x1 = int(region[0] * image_width / IMAGE_SIZE[0]) y1 = int(region[1] * image_height / IMAGE_SIZE[1]) x2 = int((region[0] + region[2]) * image_width / IMAGE_SIZE[0]) y2 = int((region[1] + region[3]) * image_height / IMAGE_SIZE[1]) # filename contains test_images/ which is not there in test.csv test.loc[filename[12:], 'x1'] = x1 test.loc[filename[12:], 'x2'] = x2 test.loc[filename[12:], 'y1'] = y1 test.loc[filename[12:], 'y2'] = y2 except: print(count, filename) test.to_csv('predictions_r3_xcep_model1.csv', encoding='utf-8', index=True)
def df_to_image_array_xd(df, size, lw=3, time_color=True): df['drawing1'] = df['drawing'].apply(ast.literal_eval) x = np.zeros((len(df), size, size, 1)) for i, raw_strokes in enumerate(df.drawing1.values): x[i, :, :, 0] = draw_cv2(raw_strokes, size=size, lw=lw, time_color=time_color) x = np.repeat(x, 3, axis =3) x = preprocess_input(x).astype(np.float32) df['drawing'] = df['drawing'].map(_stack_it) x2 = np.stack(df['drawing'], 0) return [x,x2]
def xception_feature_extractor(preprocess_image): # preprocessing for image input the vgg_server # preprocess_image = nn_image_preprocessing(image_bytes) processed_imgs = xception.preprocess_input(preprocess_image) # predicting the image inception_resnet_v2_features = xception_extractor.predict(processed_imgs) # making features flatten and reshape flattened_features = inception_resnet_v2_features.flatten() flattened_features = np.array(flattened_features) flattened_features = flattened_features.reshape(1, -1) return sparse.csr_matrix(flattened_features)
def image_tagging(x, model, tag_name): output = {} x = xception.preprocess_input(x) x = numpy.array([x]) x = base_model_Xception.predict(x) y_score = model.predict(x) prediction = numpy.argmax(y_score) score = numpy.max(y_score) if prediction > 0: output["tag"] = tag_name output["score"] = score return output
def prepare_model(image_path, model): # resize the input image and preprocess it img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # predict predict = model.predict(x) # return the processed plot return predict
def get_model(): inputs = Input(shape=(608, 608, 3)) x = Lambda(lambda image: preprocess_input(image))(inputs) xception = Xception(weights='imagenet', input_shape=(608, 608, 3), include_top=False) tail_prev = xception.get_layer('block13_pool').output tail = xception.output output = FinalModel(x, tail_prev, tail) return inputs, xception.input, output