def call(self, inputs, training=None, mask=None): anchor_in, positive_in, negative_in = inputs distances = self.distance( self.embedding(resnet.preprocess_input(anchor_in)), self.embedding(resnet.preprocess_input(positive_in)), self.embedding(resnet.preprocess_input(negative_in)), ) return distances
def loadAndPrep100(): # x data in form of (num samples, num channels, width, height) = (50000 or 10000, 32, 32, 3) # both in uint8 (xtrain, ytrain), (xtest, ytest) = cifar100.load_data() # # use one-hot encoding for y ytrain = np_utils.to_categorical(ytrain) ytest = np_utils.to_categorical(ytest) # this is for the resnet only (since vgg was abandoned) # changes pixel values to be within a certain range xtrain = preprocess_input(xtrain) xtest = preprocess_input(xtest) return xtrain, ytrain, xtest, ytest
def extract_feature(X, cnn_arch="resnet50"): if cnn_arch == "resnet50": from keras.applications.resnet import preprocess_input cnn = ResNet50(include_top=False, weights='imagenet') elif cnn_arch == "resnet50v2": from keras.applications.resnet_v2 import preprocess_input cnn = ResNet50V2(include_top=False, weights='imagenet') elif cnn_arch == "inceptionv3": from keras.applications.inception_v3 import preprocess_input cnn = InceptionV3(include_top=False, weights='imagenet') X = np.array([resize(X, (299, 299, 3)) for x in X]) import ipdb ipdb.set_trace() else: raise ValueError(f"Not supported cnn_arch {cnn_arch}") input = Input(shape=X.shape[1:], name='image_input') x = cnn(input) x = Flatten()(x) model = Model(inputs=input, outputs=x) X = preprocess_input(X) return model.predict(X, batch_size=64)
def preprocess_img(img): img = Image.open(img) img = img.resize((224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) #Normalisation return img
def predict(): #print('Recibo mierdas') if 'Content-Type' not in request.headers or 'multipart/form-data' not in request.headers[ 'Content-Type']: return "Content-Type wasn't 'multipart/form-data'", 400 print(request) try: print(request.files) formFile = request.files['file'] except: return "FormData didn't include a file", 400 try: print(formFile) img = np.array(resize(formFile)) print(img.shape) except: return 'Unable to read the image file', 400 img = np.expand_dims(img, axis=0) img = preprocess_input(img) #print("Let's start predicting") value = model.predict(img) K.clear_session() prediction = output[np.argmax(value)] if np.max(value) > TH else None #print("Value predicted: {}".format(prediction)) return jsonify({"prediction": prediction})
def encode(array): # this will convert cv2 arrays into a format readable to the VGG NN array = cv2.resize(array, dsize=(224,224), interpolation=cv2.INTER_CUBIC) array = cv2.cvtColor(array, cv2.COLOR_BGR2RGB) if array.dtype == 'uint8': array = array.astype(dtype='float32') array = np.expand_dims(array, axis=0) array = preprocess_input(array) return vgg_model.predict(array)[0,:]
def map(self, img_path: str) -> JSONSerializableType: img = image.load_img(img_path, target_size=None) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) feature_tensor = self.model.predict(img_data) features = ResNet152Mapper._global_max_pool_1D(feature_tensor) return features
def image_preprocessing(path, mode='train'): features = [] labels = [] train_datagen = ImageDataGenerator( rotation_range=40, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) for i, class_name in enumerate(class_names): class_path = path + class_name count = 1 for image_path in glob.glob(class_path + "/*.jpg"): # Load and resize image im = image.load_img(image_path, target_size=image_size) # Convert to numpy array x = image.img_to_array(im) # Expand dimensions for further preprocessing x = np.expand_dims(x, axis=0) # Data augmentation aug_cnt = 0 if( mode == 'test' ): pass else: datagen = train_datagen for batch in datagen.flow(x, batch_size=1): batch = preprocess_input(batch) features.append(batch) labels.append(class_name) aug_cnt += 1 if aug_cnt >= 1: break x = preprocess_input(x) features.append(x) labels.append(class_name) count += 1 print("[INFO] completed image - " + image_path) print("[INFO] completed label - " + class_name) return features, labels
def _getImageFeatures(model, img_path): img = image.load_img(img_path, target_size=None) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) feature_tensor = model.predict(img_data) get_img_id = lambda p: p.split('/')[-1].split('.')[0] return { "id": get_img_id(img_path), "features": _globalMaxPool1D(feature_tensor), }
def encode_img(self, filename): if self.model == None: return None img_width, img_height = 256, 256 img = image.load_img(filename, target_size = (img_width, img_height)) img = image.img_to_array(img) img = np.expand_dims(img, axis = 0) if self.modelType in [ModelType.ResNet50, ModelType.ResNet101]: img = resnet.preprocess_input(img) if self.modelType in [ModelType.DenseNet121, ModelType.DenseNet169]: img = densenet.preprocess_input(img) return self.model.predict(img)
def test(self): model = load_model(self.model_save_path) image = load_img(self.test_image_path, target_size=(224, 224)) image1 = img_to_array(image) image1 = image1.reshape( (1, image1.shape[0], image1.shape[1], image1.shape[2])) image1 = preprocess_input(image1) yhat = model.predict(image1) pred = np.argmax(yhat) #change according to your dataset if pred == 0: prediction = 'satellite_image' print(prediction) plt.imshow(image) else: prediction = 'non_satellite_image' print(prediction) plt.imshow(image)
def __getitem__(self, index): 'Generate one batch of data' # Generate indexes of the batch X_id_batch = self.X_id[index * self.batch_size:(index + 1) * self.batch_size] y_batch = self.y[index * self.batch_size:(index + 1) * self.batch_size] # Generate data X = [] y = [] for x_p, y_p in zip(X_id_batch, y_batch): img_or = imread("images/" + x_p) img_or = cv2.cvtColor(img_or, cv2.COLOR_BGR2RGB) p = np.random.rand() if self.train: img_or = seq.augment_image(img_or) if p > 0.8: img_or = cv2.cvtColor(img_or, cv2.COLOR_RGB2GRAY) img_or = cv2.cvtColor(img_or, cv2.COLOR_GRAY2RGB) scale = np.random.randint(256, 300) img = image_resize(img_or, scale) if self.train: center_ = center(img, 224) top_left_ = top_left(img, 224) top_right_ = top_right(img, 224) bottom_right_ = bottom_right(img, 224) bottom_left_ = bottom_left(img, 224) X.append(preprocess_input(center_)) X.append(preprocess_input(top_left_)) X.append(preprocess_input(top_right_)) X.append(preprocess_input(bottom_right_)) X.append(preprocess_input(bottom_left_)) y.append(y_p) y.append(y_p) y.append(y_p) y.append(y_p) y.append(y_p) else: img_ = randomCrop(img, 224) X.append(preprocess_input(img_)) y.append(y_p) return np.array(X), np.array(y)
def __getitem__(self, idx): image = cv2.imread(self.X[idx]) # report details about the image # print(type(image)) # print(image.format) # print(image.mode) # print(image.shape) # convert to numpy array # img_array = img_to_array(image) # print(img_array.dtype) # print(img_array.shape) # Image size height, width = image.shape[:2] # 2. Detect with dlib gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) face_detector = dlib.get_frontal_face_detector() faces = face_detector(gray, 1) #if there are faces if len(faces): # For now only take biggest face face = faces[0] # Face crop with dlib and bounding box scale enlargement x, y, size = get_boundingbox(face, width, height) # print("size of the crop is: " + str(size)) cropped_face = image[y:y + size, x:x + size] resized = cv2.resize(cropped_face, (299, 299), interpolation=cv2.INTER_AREA) resized = np.expand_dims(resized, axis=0) resized = preprocess_input(resized) # print(resized.shape) # return cropped faces return resized, self.Y[idx]
# In[8]: from keras.applications.resnet import preprocess_input datagen = ImageDataGenerator(horizontal_flip=True, preprocessing_function=preprocess_input) # In[9]: len(model_arr) # In[10]: # from keras.applications.vgg16 import preprocess_input from keras.applications.resnet import preprocess_input x_train_ = preprocess_input(x_train) x_val_ = preprocess_input(x_val) # In[22]: hist = [] nb_epoch = 3 for i in range(model_count): hist_tmp = model_arr[i].fit_generator( datagen.flow(x_train, y_train, batch_size=16), steps_per_epoch=500, epochs=nb_epoch, #Increase this when not on Kaggle kernel verbose=1, #1 for ETA, 0 for silent validation_data=(x_val_, y_val)) #validation_data=(x_val_, y_val)For speed hist.append(hist_tmp)
def train(dataset, architecture, task_name): ROOT_MODELS = '/home/dembanakh/.ml-manager/tasks-weights/' ROOT_DATASETS = '/home/dembanakh/.ml-manager/datasets/' if dataset == 'IMAGENET': if architecture == 'VGG16': from keras.applications.vgg16 import VGG16 model = VGG16(weights='imagenet') elif architecture == 'VGG19': from keras.applications.vgg19 import VGG19 model = VGG19(weights='imagenet') elif architecture == 'MobileNet': from keras.applications.mobilenet import MobileNet model = MobileNet(weights='imagenet') elif architecture == 'ResNet': from keras.applications.resnet import ResNet50, preprocess_input model = ResNet50(weights='imagenet') elif architecture == 'DenseNet': from keras.applications.densenet import DenseNet121, preprocess_input model = DenseNet121(weights='imagenet') else: return 0 model.compile(optimizer='adam', metrics=['accuracy'], loss='sparse_categorical_crossentropy') model.save(ROOT_MODELS + task_name + '.h5') else: input_shape = (224, 224, 3) batch_size = 1 # subject to change, but Azure server has little RAM import os import numpy as np from keras.preprocessing import image try: samples = [i for i in os.listdir(dataset + '/samples')] except OSError: print 'There is no such directory', dataset + '/samples' return 0 X = np.zeros((len(samples), input_shape[0], input_shape[1], input_shape[2])) # maybe depends on architecture y = np.zeros((len(samples), )) if architecture == 'VGG16': from keras.applications.vgg16 import VGG16, preprocess_input model = VGG16() for i in range(X.shape[0]): X[i] = preprocess_input(X[i]) elif architecture == 'VGG19': from keras.applications.vgg19 import VGG19, preprocess_input model = VGG19() for i in range(X.shape[0]): X[i] = preprocess_input(X[i]) elif architecture == 'MobileNet': from keras.applications.mobilenet import MobileNet, preprocess_input model = MobileNet() for i in range(X.shape[0]): X[i] = preprocess_input(X[i]) elif architecture == 'ResNet': from keras.applications.resnet import ResNet50, preprocess_input model = ResNet50() for i in range(X.shape[0]): X[i] = preprocess_input(X[i]) elif architecture == 'DenseNet': from keras.applications.densenet import DenseNet121, preprocess_input model = DenseNet121() for i in range(X.shape[0]): X[i] = preprocess_input(X[i]) else: return 0 for i, sample in enumerate(samples): try: img = image.load_img(dataset + '/samples/' + sample, target_size=input_shape) except IOError: print 'Failed to open file', dataset + '/samples/' + sample return 0 try: f_lbl = open( dataset + '/labels/' + sample.split('.')[0] + '.txt', 'r') except IOError: print 'Failed to open file', dataset + '/labels/' + sample.split( '.')[0] + '.txt' return 0 try: y[i] = int(f_lbl.read()) except ValueError: print 'File', dataset + '/labels/' + sample.split( '.')[0] + '.txt', 'doesn\'t contain integer' return 0 model.compile(optimizer='adam', metrics=['accuracy'], loss='sparse_categorical_crossentropy') model.fit(X, y, batch_size=batch_size) model.save(ROOT_MODELS + task_name + '.h5') return 1
# print(x_pred.shape) # 50000,256,256 y = pd.read_csv('../data/csv/Dacon3/dirty_mnist_2nd_answer.csv') sub = pd.read_csv('../data/csv/Dacon3/sample_submission.csv') y = y.iloc[:, 1:] # x = x[:50000,:,:] # # y = y['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', # # 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] y = y.to_numpy() #전처리 x = x.reshape(-1, 128, 128, 3) x_pred = x_pred.reshape(-1, 128, 128, 3) x = preprocess_input(x) x_pred = preprocess_input(x_pred) x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.9) # 이미지 증폭 idg = ImageDataGenerator( # rotation_range=10, acc 하락 width_shift_range=(-1, 1), # 0.1 => acc 하락 height_shift_range=(-1, 1), # 0.1 => acc 하락 zoom_range=0.2) # rotation_range=40, # shear_range=0.2, # 현상유지 # zoom_range=0.2) # horizontal_flip=True)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from keras.applications import ResNet50 from keras.applications.resnet import preprocess_input, decode_predictions # Get a pre-built ResNet50 model model = ResNet50(weights='imagenet') # Read the image into memory as a numpy array image = cv2.imread('elephant.jpg', cv2.IMREAD_COLOR) # Resize the image to fit the input shape of ResNet model image = cv2.resize(image, (224, 224), cv2.INTER_LINEAR) # Preprocess the image using the same image processing used by the pre-built model image = preprocess_input(image) # Reshape from (224, 224, 3) to (1, 224, 224, 3) for the predict() method image = image.reshape((-1, 224, 224, 3)) # Call the predict() method to classify the image predictions = model.predict(image) # Display the class name based on the predicted label using the decode function for # the built-in model. print(decode_predictions(preds, top=3))
from keras.applications import resnet from keras.preprocessing import image from keras.models import load_model from keras.activations import relu, softmax import keras.backend as K import matplotlib.pyplot as plt model = load_model('model.h5') img_path = sys.argv[1] img = image.load_img(img_path, target_size=(224, 224)) # Create a batch and preprocess the image x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = resnet.preprocess_input(x) # Get the initial predictions preds = model.predict(x) # Inverse of the preprocessing and plot the image def plot_img(x, i): """ x is a BGR image with shape (? ,224, 224, 3) """ t = np.zeros_like(x[0]) t[:, :, 0] = x[0][:, :, 2] t[:, :, 1] = x[0][:, :, 1] t[:, :, 2] = x[0][:, :, 0] plt.imshow(np.clip((t + [123.68, 116.779, 103.939]), 0, 255) / 255)
print(i, layer.name, layer.output.shape) # redefine model to output right after the first hidden layer model = Model(inputs=model.inputs, outputs=model.layers[1].output) # load the image with the required shape img = load_img('/home/yizi/Documents/phd/map/sample.png', target_size=(224, 224)) # convert the image to an array img = img_to_array(img) # expand dimensions so that it represents a single 'sample' img = np.expand_dims(img, axis=0) # prepare the image (e.g. scale pixel values for the vgg) img = preprocess_input(img) # get feature map for first hidden layer feature_maps = model.predict(img) # plot all 64 maps in an 8x8 squares square = 8 ix = 1 for _ in range(square): for _ in range(square): # specify subplot and turn of axis ax = pyplot.subplot(square, square, ix) ax.set_xticks([]) ax.set_yticks([]) # plot filter channel in grayscale pyplot.imshow(feature_maps[0, :, :, ix - 1], cmap='gray')
custom_objects = { "FaceNet": FaceNet, "DistanceLayer": DistanceLayer, "resnet": resnet } model = tf.keras.models.model_from_json(json_config, custom_objects=custom_objects) model.load_weights(config.CKPT_DIR) if __name__ == "__main__": '''calculation of the similarity (Cosine Similarity)''' samples = next(iter(val_data)) anchor, pos, neg = samples anchor_embedding = model.embedding(resnet.preprocess_input(anchor)) pos_embedding = model.embedding(resnet.preprocess_input(pos)) neg_embedding = model.embedding(resnet.preprocess_input(neg)) print( f"The shape of embeddings :{anchor_embedding.shape, pos_embedding.shape, neg_embedding.shape}" ) similarity = metrics.CosineSimilarity() pos_sim = similarity(anchor_embedding, pos_embedding) neg_sim = similarity(anchor_embedding, neg_embedding) assert pos_sim.numpy() > neg_sim.numpy() print(f"The similarity between positive image and anchor:{pos_sim}")
# Onehot encoding for class labels train_labels_onehot = to_categorical(train_labels) test_labels_onehot = to_categorical(test_labels) # make images fake rgb for ResNet 50 def fake_rgb(x): return np.repeat(x[..., np.newaxis], 3, -1) train_images = fake_rgb(train_images) test_images = fake_rgb(test_images) # preprocess data for resnet train_images = preprocess_input(train_images) # Load the model model = ResNet50(include_top=False, weights=None, input_shape=train_images[0].shape, classes=10) # Make it ready for classification flat1 = Flatten()(model.layers[-1].output) class1 = Dense(1024, activation='relu')(flat1) output = Dense(10, activation='softmax')(class1) # define new model model = Model(inputs=model.inputs, outputs=output)
break # if the frame dimensions are empty, grab them if W is None or H is None: (H, W) = frame.shape[:2] # clone the output frame, then convert it from BGR to RGB # ordering, resize the frame to a fixed 224x224, and then # perform mean subtraction output = frame.copy() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if args['predictions'] == 'weather': frame = cv2.resize(frame, (224, 224)).astype("float32") else: frame = cv2.resize(frame, (512, 512)).astype("float32") frame = preprocess_input(frame) # make predictions on the frame and then update the predictions # queue preds = model.predict(np.expand_dims(frame, axis=0))[0] Q.append(preds) # perform prediction averaging over the current history of # previous predictions results = np.array(Q).mean(axis=0) i = np.argmax(results) label = label_dict[str(i)] # draw the condition on the output frame text = "condition: {}".format(label) cv2.putText(output, text, (35, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.25,
except: pass np.save("../data/npy/croll_pred.npy", arr=img1) ##########데이터 로드 x_train, x_test, y_train, y_test = np.load("../data/npy/crolling.npy", allow_pickle=True) x_pred = np.load("../data/npy/croll_pred.npy", allow_pickle=True) print(y_train.shape) print(x_train.shape) #강아지 품종분류에 사용했던 데이터셋 # x_pred = np.load("../data/npy/P_project_x4.npy",allow_pickle=True) #일반화 x_train = preprocess_input(x_train) x_test = preprocess_input(x_test) x_pred2 = preprocess_input(x_pred) # resnet101 = ResNet101(include_top=False,weights='imagenet',input_shape=x_train.shape[1:]) # resnet101.trainable = False # x = resnet101.output # x = GlobalAveragePooling2D() (x) # x = Flatten() (x) # x = Dense(128) (x) # x = BatchNormalization() (x) # x = Activation('relu') (x) # x = Dense(64) (x) # x = BatchNormalization() (x) # x = Activation('relu') (x)
model_NasMobile = NASNetMobile(include_top=False, weights='imagenet') from keras.preprocessing.image import load_img, img_to_array import os path_cat = '/Users/mingyuexu/PycharmProjects/vgg_data_cat_c' img_name_cat = os.listdir(path_cat) data_cat = [] data_dog = [] data_cat1 = [] data_dog1 = [] for item in img_name_cat: img = load_img(f'/Users/mingyuexu/PycharmProjects/vgg_data_cat_c/{item}', target_size=(224, 224)) img_array = img_to_array(img) img_array = np.expand_dims(img_array, axis=0) X = preprocess_input(img_array) X1 = p1(img_array) data_cat.append(X) data_cat1.append(X1) data_cat = np.array(data_cat) data_cat1 = np.array(data_cat1) # print(data_cat.shape) path_dog = '/Users/mingyuexu/PycharmProjects/vgg_data_dog_c' img_name_dog = os.listdir(path_dog) for item in img_name_dog: img = load_img(f'/Users/mingyuexu/PycharmProjects/vgg_data_dog_c/{item}', target_size=(224, 224)) img_array = img_to_array(img) img_array = np.expand_dims(img_array, axis=0) X = preprocess_input(img_array)
def infern(self, img: np.ndarray): x = np.expand_dims(img, axis=0) x = preprocess_input(x) predictions = self.model.predict(x) return decode_predictions(predictions, top=3)