parser.add_argument('-occl_size', '--occlusion_size', help="occlusion size", default=(50, 50), type=tuple) args = parser.parse_args() # input image dimensions img_rows, img_cols = 224, 224 input_shape = (img_rows, img_cols, 3) # define input tensor as a placeholder input_tensor = Input(shape=input_shape) # load multiple models sharing same input tensor K.set_learning_phase(0) model1 = VGG16(input_tensor=input_tensor) model2 = VGG19(input_tensor=input_tensor) model3 = ResNet50(input_tensor=input_tensor) # init coverage table model_layer_dict1, model_layer_dict2, model_layer_dict3 = init_coverage_tables(model1, model2, model3) # ============================================================================================== # start gen inputs img_paths = image.list_pictures('./seeds/', ext='jpeg') for _ in xrange(args.seeds): gen_img = preprocess_image(random.choice(img_paths)) orig_img = gen_img.copy() # first check if input already induces differences pred1, pred2, pred3 = model1.predict(gen_img), model2.predict(gen_img), model3.predict(gen_img) label1, label2, label3 = np.argmax(pred1[0]), np.argmax(pred2[0]), np.argmax(pred3[0]) if not label1 == label2 == label3: print(bcolors.OKGREEN + 'input already causes different outputs: {}, {}, {}'.format(decode_label(pred1), decode_label(pred2),
target_size=image_size, batch_size=batch_size) validation_datagen = ImageDataGenerator(rescale=1./255) validation_generator = validation_datagen.flow_from_dataframe( validate_df, '/tmp/food_train/', x_col='filename', y_col='category', class_mode='categorical', target_size=image_size, batch_size=batch_size) # use ResNet50 model as baseline and remove the fully connected layers pre_trained_model = ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=(image_size[0],image_size[1],3)) x = pre_trained_model.output x = Flatten()(x) x = Dropout(0.5)(x) # add Dense layer and softmax output_layer = Dense(num_classes, activation='softmax', name='softmax')(x) # set freeze layers and trained layers model = Model(inputs=pre_trained_model.input, outputs=output_layer) for layer in pre_trained_model.layers[:freeze_layers]: layer.trainable = False for layer in pre_trained_model.layers[freeze_layers:]: layer.trainable = True
def resnet(op_from_layers=[79]): model = ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None) output_layers = [model.layers[i].output for i in op_from_layers] model_req = Model(input=model.input, output=output_layers) return model_req
euclidean(n2, n3) # In[9]: euclidean(n3, n4) # In[ ]: from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from scipy.misc import face import numpy as np resnet_settings = {'include_top': False, 'weights': 'imagenet'} resnet = ResNet50(**resnet_settings) img = image.array_to_img(face()) # какой милый енот! img = img.resize((224, 224)) # в реальной жизни может понадобиться внимательнее относиться к ресайзу x = image.img_to_array(img) x = np.expand_dims(x, axis=0) # нужно дополнительное измерение, т.к. модель рассчитана на работу с массивом изображений features = resnet.predict(x) # In[12]: import pytesseract from PIL import Image
#!/usr/bin/env python # coding: utf-8 # In[3]: from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.applications.resnet50 import preprocess_input, decode_predictions # In[4]: model = ResNet50(weights='imagenet') # In[5]: model.summary() # In[39]: img_path = 'mug.jpg' img = image.load_img(img_path, target_size=(224, 224)) # In[40]: import numpy as np x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # In[41]:
bottom = np.floor(h / 2) + np.floor(min_dim / 2) new_im = img.crop((left, top, right, bottom)) return new_im.resize((new_dim, new_dim)) #Get image idList = getList(imgFolder) topics_dict = {"id": []} #Create models model_vgg16 = VGG16(weights='imagenet', include_top=False) #model_vgg16.summary() model_vgg19 = VGG19(weights='imagenet', include_top=False) #model_vgg19.summary() model_resnet50 = ResNet50(weights='imagenet', include_top=False) #model_resnet50.summary() model_inceptv3 = InceptionV3(weights='imagenet', include_top=False) #model_inceptv3.summary() vgg16_feature_list = [] vgg19_feature_list = [] resnet50_feature_list = [] inceptv3_feature_list = [] for img in idList: imgID = img[0] imgExt = img[1] # print(imgID ) imgDir = imgFolder + '/' + imgID + imgExt
os.path.join( os.path.join( os.path.join(os.path.join(base_path, element[2]), element[1])), element[0].split('/')[0])): os.mkdir( os.path.join( os.path.join( os.path.join(os.path.join(base_path, element[2]), element[1])), element[0].split('/')[0])) shutil.move( os.path.join(base_path, element[0]), os.path.join( os.path.join(os.path.join(base_path, element[2]), element[1]), element[0])) model_resnet = ResNet50(weights='imagenet', include_top=False, pooling='avg') for layer in model_resnet.layers[:-12]: layer.trainable = False x = model_resnet.output x = Dense(512, activation='elu', kernel_regularizer=l2(0.001))(x) y = Dense(46, activation='softmax', name='img')(x) x_bbox = model_resnet.output x_bbox = Dense(512, activation='relu', kernel_regularizer=l2(0.001))(x_bbox) x_bbox = Dense(128, activation='relu', kernel_regularizer=l2(0.001))(x_bbox) bbox = Dense(4, kernel_initializer='normal', name='bbox')(x_bbox) final_model = Model(inputs=model_resnet.input, outputs=[y, bbox])
def extractFeatures(): filenames = [ 'xception-finetune.hdf5', 'resnet50-finetune.hdf5', 'inceptionv3-finetune.hdf5' ] train_gen = ImageDataGenerator() valid_gen = ImageDataGenerator() train_generator = train_gen.flow_from_directory(TRAIN_DATA_PATH, IMAGE_SIZE, shuffle=False, batch_size=BATCH_SIZE) valid_generator = valid_gen.flow_from_directory(VALID_DATA_PATH, IMAGE_SIZE, shuffle=False, batch_size=BATCH_SIZE) for filename in filenames: inputs = Input((*IMAGE_SIZE, 3)) if filename == 'xception-finetune.hdf5': x = Lambda(xception.preprocess_input)(inputs) base_model = Xception(input_tensor=x, weights='imagenet', include_top=False) elif filename == 'resnet50-finetune.hdf5': x = Lambda(resnet50.preprocess_input)(inputs) base_model = ResNet50(input_tensor=x, weights='imagenet', include_top=False) elif filename == 'inceptionv3-finetune.hdf5': x = Lambda(inception_v3.preprocess_input)(inputs) base_model = InceptionV3(input_tensor=x, weights='imagenet', include_top=False) model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output)) # 加载模型参数 model.load_weights(os.path.join(MODEL_PATH, filename), by_name=True) train_features = model.predict_generator( train_generator, steps=len(train_generator.filenames) // BATCH_SIZE, use_multiprocessing=True, workers=8, verbose=1) valid_features = model.predict_generator( valid_generator, steps=len(valid_generator.filenames) // BATCH_SIZE, use_multiprocessing=True, workers=8, verbose=1) with h5py.File('{}-output.hdf5'.format(filename[:-5]), 'w') as h: h.create_dataset('X_train', data=train_features) h.create_dataset( 'y_train', data=train_generator.classes[:( (train_generator.samples // BATCH_SIZE) * BATCH_SIZE)]) h.create_dataset('X_val', data=valid_features) h.create_dataset( 'y_val', data=valid_generator.classes[:( (valid_generator.samples // BATCH_SIZE) * BATCH_SIZE)])
def main(): # Name of this script script_name = os.path.basename(__file__)[0:-3] # Construct folder name using name of this script output_path_name = '_{}_outputs'.format(script_name) # Try to create a new folder try: # Make the output folder os.mkdir(output_path_name) except FileExistsError: pass # Model below this line ================================================ learn_rate = LearningRateScheduler(best_lr_decay, verbose=1) custom_callback = get_custom_callback('multi_label', './{}'.format(output_path_name)) callbacks_list = [custom_callback, learn_rate] file = h5py.File( '/albona/nobackup/andrewl/process/old_data_rgb_512_processed.h5', 'r') x_train, y_train, x_test, y_test = file['x_train'], file['y_train'], file[ 'x_test'], file['y_test'] y_train = to_categorical(y_train, NUM_CLASSES) y_test = to_categorical(y_test, NUM_CLASSES) y_train = to_multi_label(y_train) y_test = to_multi_label(y_test) datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=360) model = Sequential() resnet = ResNet50(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3)) model.add(resnet) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(NUM_CLASSES, activation='sigmoid')) model.summary() model.compile( loss='binary_crossentropy', # optimizer=optimizers.Adam(lr=0.0001,decay=1e-6), optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), metrics=[multi_label_acc, f1_m]) # fits the model on batches with real-time data augmentation: history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=BATCH_SIZE, seed=1), steps_per_epoch=len(x_train) // BATCH_SIZE, epochs=NUM_EPOCHS, validation_data=(x_test, y_test), callbacks=callbacks_list, max_queue_size=2)
showPredict(img, preds_top5) print('\nWaiting visualize....') visualizeModel(model, imgData, block_name='block1_pool') print('\n+++++Example 3: Image classification with ResNet50 model++++++') # The default input size for ResNet50 model is 224x224. img = image.load_img('bicycle.jpg', target_size=(224, 224)) imgData = prepareImage(img, 'ResNet50') """ TH_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels.h5' TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5' TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels_notop.h5' TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' """ model = ResNet50(include_top=True, weights='imagenet') preds_top5 = getPreds_top5(model, imgData) showPredict(img, preds_top5) print('\n+++++Example 4: Image classification with InceptionV3 model++++++') #The default input size for InceptionV3 model is 299x299. img = image.load_img('bird.jpg', target_size=(299, 299)) imgData = prepareImage(img, 'InceptionV3') """ TH_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_th_dim_ordering_th_kernels.h5' TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_tf_dim_ordering_tf_kernels.h5' TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_th_dim_ordering_th_kernels_notop.h5' TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5' """ model = InceptionV3(include_top=True, weights='imagenet') preds_top5 = getPreds_top5(model, imgData)
print(len(train_filenames), 'train samples') print(len(test_filenames), 'test samples') model_name = 'rotnet_resnet50_full' # number of classes nb_classes = 4 # input image shape input_shape = (224, 224, 3) # path to the weights weights_path = os.path.join(path_r, 'data', 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5') #weights_path = os.path.join(path_r, 'data', 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') # load base model base_model = ResNet50(weights = weights_path, include_top = False, input_shape = input_shape) #base_model = VGG16(weights=weights_path, include_top=False, # input_shape=input_shape) for layer in base_model.layers: layer.trainable = False # create the new model x = base_model.output x = Flatten()(x) x = Dense(256, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(nb_classes, activation='softmax', name='fc4')(x) model = Model(base_model.input, x) model.summary()
def extract_Resnet50(tensor): from keras.applications.resnet50 import ResNet50, preprocess_input return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
import pandas as pd import tensorflow.keras as K import matplotlib.pyplot as pl from matplotlib.pyplot import imshow from keras.applications.resnet50 import ResNet50 from keras.models import Model model = ResNet50(include_top=False) input = model.layers[0].input # Remove the average pooling layer output = model.layers[-2].output headless_conv = Model(inputs=input, outputs=output) import cv2 as cv img = cv.imread('10.jpg') headless_conv.predict(img) def create_model_relu(): model = Sequential() model.add( Conv2D(16, (3, 3), input_shape=(224, 224, 3), padding="same", activation='relu')) model.add(Conv2D(32, (3, 3), padding="same", activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25))
def boundary_attack(): classifier = ResNet50(weights='imagenet') initial_sample = preprocess('images/original/awkward_moment_seal.png') target_sample = preprocess('images/original/bad_joke_eel.png') folder = time.strftime('%Y%m%d_%H%M%S', datetime.datetime.now().timetuple()) os.mkdir(os.path.join("images", folder)) draw(np.copy(initial_sample), classifier, folder) attack_class = np.argmax(classifier.predict(initial_sample)) target_class = np.argmax(classifier.predict(target_sample)) adversarial_sample = initial_sample n_steps = 0 n_calls = 0 epsilon = 1. delta = 0.1 # Move first step to the boundary while True: trial_sample = adversarial_sample + forward_perturbation( epsilon * get_diff(adversarial_sample, target_sample), adversarial_sample, target_sample) prediction = classifier.predict(trial_sample.reshape(1, 224, 224, 3)) n_calls += 1 if np.argmax(prediction) == attack_class: adversarial_sample = trial_sample break else: epsilon *= 0.9 while True: print("Step #{}...".format(n_steps)) print("\tDelta step...") d_step = 0 while True: d_step += 1 print("\t#{}".format(d_step)) trial_samples = [] for i in np.arange(10): trial_sample = adversarial_sample + orthogonal_perturbation( delta, adversarial_sample, target_sample) trial_samples.append(trial_sample) predictions = classifier.predict( np.array(trial_samples).reshape(-1, 224, 224, 3)) n_calls += 10 predictions = np.argmax(predictions, axis=1) d_score = np.mean(predictions == attack_class) if d_score > 0.0: if d_score < 0.3: delta *= 0.9 elif d_score > 0.7: delta /= 0.9 adversarial_sample = np.array(trial_samples)[np.where( predictions == attack_class)[0][0]] break else: delta *= 0.9 print("\tEpsilon step...") e_step = 0 while True: e_step += 1 print("\t#{}".format(e_step)) trial_sample = adversarial_sample + forward_perturbation( epsilon * get_diff(adversarial_sample, target_sample), adversarial_sample, target_sample) prediction = classifier.predict( trial_sample.reshape(1, 224, 224, 3)) n_calls += 1 if np.argmax(prediction) == attack_class: adversarial_sample = trial_sample epsilon /= 0.5 break elif e_step > 500: break else: epsilon *= 0.5 n_steps += 1 chkpts = [1, 5, 10, 50, 100, 500, 1000] if (n_steps in chkpts) or (n_steps % 500 == 0): print("{} steps".format(n_steps)) draw(np.copy(adversarial_sample), classifier, folder) diff = np.mean(get_diff(adversarial_sample, target_sample)) realdiff = np.sum( (adversarial_sample / 255 - target_sample / 255)**2)**0.5 if diff <= 1e-3 or e_step > 500: print("{} steps".format(n_steps)) print("Mean Squared Error: {}".format(diff)) draw(np.copy(adversarial_sample), classifier, folder) break print("Mean Squared Error: {}".format(diff)) print("Real Mean Squared Error: {}".format(realdiff)) print("Calls: {}".format(n_calls)) print("Attack Class: {}".format(attack_class)) print("Target Class: {}".format(target_class)) print("Adversarial Class: {}".format(np.argmax(prediction)))
from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.applications.resnet50 import preprocess_input, decode_predictions from keras.layers import Dense from keras.models import Model import numpy as np import os from PIL import Image import pandas as pd subm = pd.read_csv('sample_submission.csv') #labels = pd.read_csv('train/labels.csv') #labels = labels.set_index('id') model = ResNet50(weights='imagenet', include_top=True) #base_model = ResNet50(weights='imagenet',include_top = True, pooling = 'avg') #predictions = Dense(120, activation='softmax')(base_model.output) #model = Model(inputs=base_model.input, outputs=predictions) img_dir = 'test/' img_list = os.listdir(img_dir) col = subm.columns sub = pd.DataFrame(np.zeros([len(img_list), len(col)]), columns=col) sub['id'] = subm['id'] sub = sub.set_index('id') cont = 0 for img_path in img_list: img = image.load_img(img_dir + img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0)
def ResNet50_predict_labels(img_path): ResNet50_model_ = ResNet50(weights='imagenet') # returns prediction vector for image located at img_path img = preprocess_input(path_to_tensor(img_path)) return np.argmax(ResNet50_model_.predict(img))
train_labels[img_id:img_id + 1], batch_size=1) women = [next(women_generator) for i in range(0, 5)] fig, ax = plt.subplots(1, 5, figsize=(15, 6)) print('Labels:', [item[1][0] for item in women]) l = [ax[i].imshow(women[i][0][0]) for i in range(0, 5)] train_generator = train_datagen.flow(train_imgs, train_labels_enc, batch_size=30) val_generator = val_datagen.flow(val_imgs, validation_labels_enc, batch_size=30) restnet = ResNet50(include_top=False, weights='imagenet', input_shape=(300, 300, 3)) output = restnet.layers[-1].output output = keras.layers.Flatten()(output) restnet = Model(restnet.input, output=output) # for layer in restnet.layers: # layer.trainable = False restnet.summary() restnet.trainable = True set_trainable = False for layer in restnet.layers: if layer.name in ['res5c_branch2b', 'res5c_branch2c', 'activation_49']: set_trainable = True if set_trainable: layer.trainable = True
load_atlas=True, use_estimated_3DBB=True, estimated_3DBB_path=estimated_3DBB) snapshots_dir = os.path.join(cache, "snapshots") tensorboard_dir = os.path.join(cache, "tensorboard") ###build training model if using_VGG: main_input = Input(shape=input_shape, name='main_input') model_main = VGG_model() x = model_main(main_input) elif using_resnet: model_main = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = Flatten()(model_main.output) direction_output = Dense(direction_number, activation='softmax', name='output_d')(x) angle_output = Dense(angle_bin_number, activation='softmax', name='output_a')(x) if using_VGG: model = Model(inputs=main_input, outputs=[direction_output, angle_output]) elif using_resnet: model = Model(inputs=model_main.input, outputs=[direction_output, angle_output])
def run(epochs, batch_size, lr, gpus, data_augmentation, cpu_relocation, cpu_merge): time_reporter = StepTimeReporter() # img_arr is of shape (n, h, w, c) def resize_image_arr(img_arr): x_resized_list = [] for i in range(img_arr.shape[0]): img = img_arr[i] resized_img = resize(img, (224, 224)) x_resized_list.append(resized_img) return np.stack(x_resized_list) (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train[:2048] y_train = y_train[:2048] x_test = x_test[:2048] y_test = y_test[:2048] print("Resizing images") # Resize image arrays x_train = resize_image_arr(x_train) x_test = resize_image_arr(x_test) NUM_CLASSES = 10 # Convert class vectors to binary class matrices. y_train = keras.utils.to_categorical(y_train, NUM_CLASSES) y_test = keras.utils.to_categorical(y_test, NUM_CLASSES) # Normalize the data x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 model = ResNet50(include_top=True, weights=None, classes=10) STEPS = 2 if random.choice([True, False]): opt = keras.optimizers.SGD(lr=lr) opt = runai.ga.keras.optimizers.Optimizer(opt, STEPS) else: opt = runai.ga.keras.optimizers.SGD(STEPS, lr=lr) opt = hvd.DistributedOptimizer(opt) # Not needed to change the device scope for model definition: model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) if not data_augmentation: model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=False) else: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization= False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range= 0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0, # randomly shift images horizontally (fraction of total width) height_shift_range= 0, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for feature-wise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). model.fit_generator( datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=int(10 * x_train.shape[0] / batch_size), epochs=epochs, validation_data=(x_test, y_test), workers=1, callbacks=[ time_reporter, hvd.callbacks.BroadcastGlobalVariablesCallback(0) ]) print("BYE BYE")
for i in random_index: img_path = os.path.join(DATA_PATH, train_x[i]['img_path']) img = cv2.imread(img_path) img = cv2.resize(img, (img_size[0], img_size[1])) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img / 255.0 batch_x.append(img) label_onehot = np.zeros((label_num), dtype=int) index = label_list.index(train_y[i]['label']) label_onehot[index] = 1 batch_y.append(label_onehot) yield np.array(batch_x), np.array(batch_y) resnet = ResNet50(weights=None, include_top=False, pooling='avg') path = remote_helper.get_remote_date( "https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5" ) resnet.load_weights(path) x = resnet.output x = keras.layers.Dropout(rate=0.5)(x) x = keras.layers.Dense(512, activation='relu')(x) x = keras.layers.BatchNormalization()(x) x = keras.layers.Dense(45, activation='softmax')(x) my_model = keras.models.Model(inputs=resnet.input, outputs=x) my_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
loss_config["gan_training"] = "mixup_LSGAN" loss_config['use_PL'] = False loss_config["PL_before_activ"] = True loss_config['use_mask_hinge_loss'] = False loss_config['m_mask'] = 0. loss_config['lr_factor'] = 1. loss_config['use_cyclic_loss'] = False from networks.faceswap_gan_model import FaceswapGANModel from data_loader.data_loader import DataLoader from utils import showG, showG_mask, showG_eyes model = FaceswapGANModel(**arch_config) from keras.applications.resnet50 import ResNet50 vggface = ResNet50(include_top=False, input_shape=(224, 224, 3)) model.build_pl_model(vggface_model=vggface, before_activ=loss_config["PL_before_activ"]) model.build_train_functions(loss_weights=loss_weights, **loss_config) # Create ./models directory Path(f"models").mkdir(parents=True, exist_ok=True) # Get filenames train_A = glob.glob(img_dirA + "/*.*") train_B = glob.glob(img_dirB + "/*.*") train_AnB = train_A + train_B assert len(train_A), "No image found in " + str(img_dirA) assert len(train_B), "No image found in " + str(img_dirB)
from keras.applications.resnet50 import ResNet50 from keras.models import Model from keras.layers import Input, AveragePooling2D, Flatten, Dense num_classes = 19 resnet = ResNet50( include_top=False, weights=None, input_tensor=None, ) input_tensor = Input(shape=(175, 175, 3)) x = resnet(input_tensor) x = AveragePooling2D(pool_size=4)(x) x = Flatten()(x) x = Dense(256, activation='relu')(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(x) model = Model(input_tensor, outputs) model.save('model.h5')
from keras.utils import to_categorical from keras.utils import Sequence from keras.layers import Input, Dense, Dropout, Embedding, LSTM from keras.layers.merge import add # In[2]: model = load_model('model_29.h5') # In[3]: model_temp = ResNet50(weights='imagenet',input_shape=(224,224,3)) # In[4]: model_resnet = Model(model_temp.input,model_temp.layers[-2].output) # In[7]: def preprocess_img(img): img = image.load_img(img,target_size=(224,224)) img = image.img_to_array(img) img = np.expand_dims(img,axis=0)
import cv2 import dlib import os.path from keras.models import Sequential from keras.applications.resnet50 import ResNet50 PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) model_path = os.path.join(PACKAGE_ROOT, "face_detector.dat") resnet_model_path = os.path.join(PACKAGE_ROOT, "model-resnet.h5") cnn_face_detector = dlib.cnn_face_detection_model_v1(model_path) resnet = ResNet50(include_top=False, pooling='avg') model = Sequential() model.add(resnet) model.add(Dense(5, activation='softmax')) model.layers[0].trainable = False model.load_weights(resnet_model_path) def score_mapping(modelScore): if modelScore <= 3.4: mappingScore = 5 / 3 * modelScore + 5 / 6 elif modelScore <= 4: mappingScore = 5 / 2 * modelScore - 2 elif modelScore < 5: mappingScore = modelScore + 4
from keras.applications.resnet50 import ResNet50 from keras.models import Model from keras.initializers import RandomUniform from keras.regularizers import l2 from keras import layers from project.model.layers.anchorage import Anchorage from project.model.layers.supression import Suppression RESNET = ResNet50(weights='imagenet', include_top=False, input_shape=(300, 300, 3)) initializer = RandomUniform(minval=-1e-2, maxval=1e-2) # activation = layers.LeakyReLU(alpha=0.1) activation = 'relu' def ssd_model_300_resnet(num_classes=600, reg=0.00003, inference=False): conv9_1 = layers.Conv2D(128, (1, 1), activation=activation, padding='same', kernel_regularizer=l2(reg), kernel_initializer=initializer, name='conv9_1')(RESNET.output) conv9_2 = layers.Conv2D(256, (3, 3), strides=(2, 2), activation=activation, padding='same',
img """### Import model - Currently, seven models are supported - Xception - VGG16 - VGG19 - ResNet50 - InceptionV3 - InceptionResNetV2 - MobileNet """ from keras.applications.resnet50 import ResNet50 model_resnet50 = ResNet50() img_resnet50_pth = '/content/drive/My Drive/Colab Notebooks/data/image/cat.jpg' img_resnet50 = load_img(img_resnet50_pth, target_size=( 224, 224)) # image size can be calibrated with target_size parameter img_resnet50 img_resnet50 = img_to_array(img_resnet50) print(img_resnet50.shape) img_resnet50 = np.expand_dims(img_resnet50, axis=0) print(img_resnet50.shape) img_resnet50 = preprocess_input( img_resnet50) # preprocess image with preprocess_input function print(img_resnet50.shape)
from keras.applications.vgg16 import preprocess_input preprocessing_function = preprocess_input base_model = VGG16(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "VGG19": from keras.applications.vgg19 import preprocess_input preprocessing_function = preprocess_input base_model = VGG19(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "ResNet50": from keras.applications.resnet50 import preprocess_input preprocessing_function = preprocess_input base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "InceptionV3": from keras.applications.inception_v3 import preprocess_input preprocessing_function = preprocess_input base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "Xception": from keras.applications.xception import preprocess_input preprocessing_function = preprocess_input base_model = Xception(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "InceptionResNetV2": from keras.applications.inceptionresnetv2 import preprocess_input
def get_resnet(): image_shape = (224, 224, 3) resnet = ResNet50(weights='imagenet', input_shape=image_shape, include_top=False) return [resnet, 'resnet']
lr = 1e-3 ## lr = 1e-3 ## if epoch > 180: ## lr *= 0.5e-3 ## elif epoch > 160: ## lr *= 1e-3 ## elif epoch > 120: ## lr *= 1e-2 ## elif epoch > 80: ## lr *= 1e-1 print('Learning rate: ', lr) return lr model = models.Sequential() base_model = ResNet50(weights=None, include_top=False, input_shape=(32, 32, 3), pooling=None) #base_model.summary() #pdb.set_trace() #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) #model.add(layers.UpSampling2D((2,2))) model.add(base_model) model.add(layers.Flatten()) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(128, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(64, activation='relu'))
import tensorflow as tf #tf.logging.set_verbosity(tf.logging.ERROR) # warning 출력 방지 from keras.applications.resnet50 import ResNet50, decode_predictions resnet = ResNet50() resnet.summary()