def test_resnet50_variable_input_channels(): input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else (None, None, 1) model = applications.ResNet50(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 2048) input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else (None, None, 4) model = applications.ResNet50(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 2048)
def save_bottlebeck_features(): #datagen = ImageDataGenerator(rescale=1. / 255) datagen = ImageDataGenerator(rescale=1., featurewise_center=True) #(rescale=1./255) datagen.mean=np.array([103.939, 116.779, 123.68],dtype=np.float32).reshape(1,1,3) # build the ResNet50 network model = applications.ResNet50(include_top=False, pooling = 'avg', weights='imagenet') #model = applications.(include_top=False, weights='imagenet') generator = datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples // batch_size) np.save(open('bottleneck_features_train.npy', 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples // batch_size) np.save(open('bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation) #model.summary() plot_model(model, show_shapes = True,to_file='Model_ResNet_notop.png')
def model(): convnet = applications.ResNet50( weights="imagenet", include_top=False, input_shape=(config.IMAGE_SIZE, config.IMAGE_SIZE, 3), ) # custom Layers out = convnet.output out = Flatten()(out) out = Dense(256, activation="relu")(out) out = Dropout(0.5)(out) out = Dense(128, activation="relu")(out) out = Dropout(0.5)(out) out = Dense(64, activation="relu")(out) out = Dropout(0.5)(out) out = Dense(32, activation="relu")(out) out = Dropout(0.5)(out) out = Dense(16, activation="relu")(out) predictions = Dense(1, activation="sigmoid")(out) # creating the final model model = Model(inputs=convnet.input, outputs=predictions) # compile the model model.compile( loss="binary_crossentropy", optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"]) return model
def save_bottlebeck_features(): train_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) # build the ResNet50 network, width and height should be no smaller than 197 model = applications.ResNet50(include_top=False, weights='imagenet') train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( train_generator, aug_factor * nb_classes * nb_train_samples // batch_size) train_labels = np.tile(np.repeat(np.arange(nb_classes), nb_train_samples), aug_factor) np.savez(bn_train_path, data=bottleneck_features_train, label=train_labels) test_generator = test_datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( test_generator, nb_classes * nb_validation_samples // batch_size) validation_labels = np.repeat(np.arange(nb_classes), nb_validation_samples) np.savez(bn_validation_path, data=bottleneck_features_validation, label=validation_labels)
def predict_model(): #**** to define the model and load weights base_model = applications.ResNet50(weights=None, include_top=False, input_shape=(256, 320, 3)) add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) add_model.add(Dense(128, activation='tanh')) add_model.add(Dropout(0.3)) add_model.add(Dense(1, activation='sigmoid')) #add_model.load_weights('bottleneck_fully_connected_layer_model_for_resnet.h5') model = Model(inputs=base_model.input, outputs=add_model(base_model.output)) sgd = SGD(lr=1e-3, decay=1e-5, momentum=0.8, nesterov=True) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) model.load_weights( 'Weight_deposit/ResNet-transferlearning2_2018_4_8_3.model') model.summary() return model
def model_define(modeltype, inputshape): if modeltype == 'define': model = customize_mode() print('Model: define !') elif modeltype == 'EfficientNetB3': model = efn.EfficientNetB3(include_top=False, weights='imagenet', input_tensor=None, input_shape=inputshape, pooling=None) freeze_layers(model) print('Model: EfficientNetB3, weights loaded!') elif modeltype == 'ResNet50': model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=inputshape, pooling='avg') freeze_layers(model) print('Model: ResNet50, weights loaded!') elif modeltype == 'Xception': model = applications.Xception(include_top=False, weights='imagenet', input_shape=inputshape) freeze_layers(model) print('Model: Xception, weights loaded!') else: pass return model
def build_fit_save_cnn(input_shape, n_classes, epochs, batch_size, X_train, X_val, y_train, y_val): base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) add_model.add(Dense(512, activation='relu')) add_model.add(Dropout(0.25)) add_model.add(Dense(n_classes, activation='softmax')) # combine base model and fully connected layers final_model = Model(inputs=base_model.input, outputs=add_model(base_model.output)) # specify SDG optimizer parameters sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # compile model final_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) final_model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_val, y_val)) score = final_model.evaluate(X_val, y_val, verbose=0) print('Val. score:', score[0]) print('Val. accuracy:', score[1]) save_model(final_model) return final_model
def get_features_convolve(mass_data, nonmass_data): model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=(56, 56, 3)) features_mass = [] features_nonmass = [] for i in range(len(mass_data)): x = mass_data[i] file_name = "C:/Srp 2018/PNGs/mass" + str(i) + ".png" dicomToPng(x, file_name) img = image.load_img(file_name, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) features = model.predict(x) #print(features.shape) features_mass.append(features) with open("feat_mass.p", "rb") as f: features_mass = pickle.load(f) iter = len(mass_data) for i in range(len(nonmass_data)): print(i) y = nonmass_data[i] stem = "C:/Srp 2018/PNGs/nonmass" + str(i) + "a" feat_nonmass, iter = convolve_train_noblack(y, stem, model, iter) if iter <= 0: break features_nonmass += feat_nonmass return features_mass, features_nonmass
def build_model(input_shape, pre_trained_weights, num_classes, dropout=False, model='resnet'): if model == 'densenet121': base_model = applications.DenseNet121(weights=pre_trained_weights, include_top=False, input_shape=input_shape) elif model == 'vgg16': base_model = applications.VGG16(weights=pre_trained_weights, include_top=False, input_shape=input_shape) else: base_model = applications.ResNet50(weights=pre_trained_weights, include_top=False, input_shape=input_shape) if pre_trained_weights == 'imagenet': for layer in base_model.layers: layer.trainable = False x = base_model.output x = GlobalAveragePooling2D()(x) if dropout: x = Dropout(0.3)(x) # let's add a fully-connected layer x = Dense(1024, activation='relu')(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(num_classes, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) return model
def getFeatures(mass_data, nonmass_data): model = applications.ResNet50(weights='imagenet', include_top=False) features_mass = [] features_nonmass = [] for i in range(len(mass_data)): #x = mass_data[i] file_name = "C:/Srp 2018/PNGs/mass" + str(i) + ".png" #dicomToPng(x, file_name) img = image.load_img(file_name, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) features = model.predict(x) #print(features.shape) features_mass.append(features) with open("feat_mass.p", "rb") as f: features_mass = pickle.load(f) for i in range(len(nonmass_data)): print(i) y = nonmass_data[i] file_name = "C:/Srp 2018/PNGs/nonmass" + str(i) + ".png" dicomToPng(y, file_name) img = image.load_img(file_name, target_size=(224, 224)) y = image.img_to_array(img) y = np.expand_dims(y, axis=0) features = model.predict(y) features_nonmass.append(features) return features_mass, features_nonmass
def test(): import ddsm_roi DDSM = get_file.get_full_path("C:/Srp 2018/Test-Full/CBIS-DDSM/") DDSM_ROI, DDSM = ddsm_roi.get_roi_cropped( "C:/Srp 2018/Test-ROI/CBIS-DDSM/", DDSM) model = applications.ResNet50(weights='imagenet', include_top=False) clf = joblib.load("svm_convolve_noblack.pkl") clf_prob = joblib.load("svm_convolve-noblack_prob.pkl") correct_patches = 0 num_patches = 0 true_positives = 0 false_positives = 0 true_negatives = 0 false_negatives = 0 #DDSM = ["C:/Srp 2018/Test-ROI/CBIS-DDSM/Mass-Test_P_00017_LEFT_MLO_1/10-04-2016-DDSM-27297/1-ROI mask images-18984/000001.dcm"] #DDSM = ["C:/Srp 2018/Test-Full/CBIS-DDSM/Mass-Test_P_00017_LEFT_MLO/10-04-2016-DDSM-89998/1-full mammogram images-29934/000000.dcm"] for i in range(len(DDSM)): #print(i) pixel_array = pydicom.dcmread(DDSM[i]).pixel_array stem = "C:/Srp 2018/PNG_test/" + str(i) + "a" patch_list = convolve_svm_test(pixel_array, stem, model, clf, clf_prob) #print(patch_list) #patch list is a list of tuples that contain the x index of the middle pixel of the path #the y inex of the middle pixel, and the radius of the patch test_roi_names = get_test_roi_names(DDSM[i]) #print(test_roi_names) for j in range(len(test_roi_names)): corrects = 0 true_pos = 0 false_pos = 0 true_neg = 0 false_neg = 0 for k in range(len(patch_list)): #print("in") try: if patch_list[k][3] == 1: num_patches += 1 coord_correct = correct_test.constructPatch( pydicom.dcmread(test_roi_names[j]).pixel_array, patch_list[k][0], patch_list[k][1], patch_list[k][2]) corrects, incorrects, true_pos, false_pos, true_neg, false_neg = correct_test.correct( coord_correct, patch_list[k], int(patch_list[k][3]) == 1) if corrects != 0: break except AttributeError: print("uh oh at " + test_roi_names[j]) correct_patches += corrects #print(corrects) true_positives += true_pos false_positives += false_pos true_negatives += true_neg false_negatives += false_neg print("correct patches = " + str(correct_patches)) print("num patches = " + str(num_patches)) print("accuracy = " + str(correct_patches / num_patches)) print("true positives = " + str(true_positives)) print("false positives = " + str(false_positives)) print("true negatives = " + str(true_negatives)) print("false negatives = " + str(false_negatives))
def save_bottlebeck_features(): datagen = ImageDataGenerator(rescale=1. / 255) # build the VGG16 network model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=1, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples) np.save(open('bottleneck_features_train.npy', 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=1, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples) np.save(open('bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation)
def save_bottlebeck_features(): datagen = ImageDataGenerator(rescale=1. / 255) # build the VGG16 network model = applications.ResNet50(include_top=False, weights='imagenet') generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples // batch_size) np.save(open('resnet50_bottleneck_features_train.npy', 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples // batch_size) np.save(open('resnet50_bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation) class_dictionary = generator.class_indices print(class_dictionary)
def callModel(model_picked = 'vgg16'): '''function returns the model picked based on input Input choices: 'vgg16' - VGG16 'vgg19' - VGG19 'res50' - ResNet50 'xception' - Xception 'inception' - InceptionV3 'monet' - MobileNetV2 ''' #The models have a series of convolutional layers and then they have dense(deeply connected layers) #include_top = False only gets the convo layers and ignores the dense layer #imagenet is a huge image dataset on which the models are trained. if weights ='imagenet' means the weights are acquired from that. if model_picked == 'vgg16': model = applications.VGG16(include_top=False, weights='imagenet') elif model_picked =='vgg19': model = applications.VGG19(include_top=False, weights='imagenet') elif model_picked == 'res50': model = applications.ResNet50(include_top=False, weights='imagenet') elif model_picked == 'xception': model = applications.Xception(include_top=False, weights='imagenet') elif model_picked == 'inception': model = applications.InceptionV3(include_top=False, weights='imagenet') elif model_picked == 'monet': model = applications.MobileNetV2(include_top=False, weights='imagenet', input_shape=(224,224,3)) return model
def get_transfer_model(): # import inception with pre-trained weights. do not include fully #connected layers if MODEL == "resnet50": base_model = applications.ResNet50(weights='imagenet', include_top=False) elif MODEL == "mobilenet": base_model = applications.MobileNet(weights='imagenet', include_top=False, input_shape=input_shape) else: raise Exception("Invalid model: '{}'".format(MODEL)) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # add a fully-connected layer x = Dense(512, activation='relu')(x) # and a fully connected output/classification layer predictions = Dense(CLASS_COUNT, activation='softmax')(x) # create the full network so we can train on it transfer_learning_model = Model(inputs=base_model.input, outputs=predictions) # create the full network so we can train on it transfer_learning_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) return transfer_learning_model
def build_cnn_resnet_50(input_shape=(224,224,3)): ''' Builds and compiles CNN with ResNet50 pre-trained model. Input: Shape of images to feed into top layers of model Output: Compiled model (final_model), summary of compiled model ''' base_model = applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) add_model.add(Dense(512, activation='relu')) # add_model.add(Dropout(0.5)) add_model.add(Dense(nb_classes, activation='softmax')) # Combine base model and my fully connected layers final_model = Model(inputs=base_model.input, outputs=add_model(base_model.output)) # Specify SGD optimizer parameters sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # Compile model final_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) return final_model, final_model.summary()
def existing_model(base_model, base_model_layer, l): if base_model == 'VGG16': base_model = applications.VGG16( weights="imagenet", include_top=False, input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3)) elif base_model == 'ResNet50': base_model = applications.ResNet50( weights="imagenet", include_top=False, input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3)) elif base_model == 'Xception': base_model = applications.Xception( weights="imagenet", include_top=False, input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3)) else: raise "Unrecognized existing model {}".format(base_model) if base_model_layer == -1: x = base_model.layers[-1].output else: for layer in base_model.layers: layer.trainable = False x = base_model.layers[base_model_layer].output x = Flatten()(x) x = Dense(512, activation="relu", kernel_regularizer=regularizers.l2(l))(x) x = Dense(128, activation="relu", kernel_regularizer=regularizers.l2(l))(x) x = Dense(2, activation='sigmoid')(x) return Model(input=base_model.input, output=x)
def save_bottlebeck_features(): datagen = ImageDataGenerator(preprocessing_function=preprocess_input_with_one_dim) # build the VGG16 network model = applications.ResNet50(include_top=False, weights='imagenet', input_shape = (img_width, img_height, 3)) generator = datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=1, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples) np.save(open('/nfs/home/pgulyaev/diploma/ocsvm/resnet50hinge/models/bottleneck_features_train.npy', 'wb'), bottleneck_features_train) generator = datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=1, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples) np.save(open('/nfs/home/pgulyaev/diploma/ocsvm/resnet50hinge/models/bottleneck_features_validation.npy', 'wb'), bottleneck_features_validation)
def save_bottleneck_features(): # build the inception network model = applications.ResNet50(include_top=False, weights='imagenet') datagen = ImageDataGenerator(preprocessing_function=preprocess_input) generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) print(len(generator.filenames)) print(generator.class_indices) print(len(generator.class_indices)) bottleneck_features_train = model.predict_generator(generator, verbose=1) np.save(bft_file, bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator(generator, verbose=1) np.save(bfv_file, bottleneck_features_validation)
def callModel(model_picked='vgg16'): '''function returns the model picked based on input Input choices: 'vgg16' - VGG16 'vgg19' - VGG19 'res50' - ResNet50 'xception' - Xception 'inception' - InceptionV3 'monet' - MobileNetV2 ''' if model_picked == 'vgg16': model = applications.VGG16(include_top=False, weights='imagenet') elif model_picked == 'vgg19': model = applications.VGG19(include_top=False, weights='imagenet') elif model_picked == 'res50': model = applications.ResNet50(include_top=False, weights='imagenet') elif model_picked == 'xception': model = applications.Xception(include_top=False, weights='imagenet') elif model_picked == 'inception': model = applications.InceptionV3(include_top=False, weights='imagenet') elif model_picked == 'monet': model = applications.MobileNetV2(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) return model
def build_model(input_shape, learning_rate): ''' Build neural network param: input shape - tuple of input size learning_rate - float initial learning rate return: keras model ''' base_model = applications.ResNet50(weights='imagenet', input_shape=input_shape, include_top=False) base_model.trainable = False input = layers.Input(input_shape) base = base_model(input, training=False) flatten_layer = layers.Flatten()(base) output = layers.Dense(1, activation='sigmoid')(flatten_layer) model = Model(inputs=input, outputs=output) optimizer = optimizers.Adam(learning_rate=learning_rate) model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) return model
def init_compile_model(state, laststate): # build the ResNet50 network initial_model = applications.ResNet50(include_top=True) # state = [1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0] initial_model = build_CNN.update_model(state, 'imagenet') pop_layer(initial_model) last = initial_model.layers[-1].output preds = Dense(200, activation='softmax', name="fc2")(last) model = Model(initial_model.input, preds) # model = load_model('current_model.h5') model.load_weights('current_model.h5') print('Model loaded.') model.summary() # pop_layer(model) # set the first x layers (up to the last conv block) # to non-trainable (weights will not be updated) trainable_index = action_generator.find_last_changed(state, laststate) for layer in model.layers[:25]: name = layer.get_config()['name'] if 'blk' not in name: layer.trainable = False elif int(name.split('blk')[1]) < trainable_index: layer.trainable = False # compile the model with a SGD/momentum optimizer # and a very slow learning rate. model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=1e-3, momentum=0.9), metrics=['accuracy']) print('Model compiled.') return model
def base_network(network='InceptionV3'): if network == 'InceptionV3': base_model = applications.InceptionV3(weights='imagenet', include_top=False) elif network == 'VGG16': base_model = applications.VGG16(weights='imagenet', include_top=False) elif network == 'VGG19': base_model = applications.VGG19(weights='imagenet', include_top=False) elif network == 'ResNet50': base_model = applications.ResNet50(weights='imagenet', include_top=False) elif network == 'InceptionResNetV2': base_model = applications.InceptionResNetV2(weights='imagenet', include_top=False) elif network == 'MobileNet': base_model = applications.MobileNet(weights='imagenet', include_top=False) elif network == 'DenseNet121': base_model = applications.DenseNet121(weights='imagenet', include_top=False) else: print('Wrong Model selected.') return None return base_model
def select_model(self, num_classes): if self.fine_tuning_rate.value != -1: if self.architecture.value == "VGG16": model = self.getVGG16Model(IMG_WIDTH, IMG_HEIGHT, num_classes, True) elif self.architecture.value == "VGG19": model = self.getVGG19Model(IMG_WIDTH, IMG_HEIGHT, num_classes, True) elif self.architecture.value == "ResNet50": model = self.getResNet50Model(IMG_WIDTH, IMG_HEIGHT, num_classes, True) for layer in model.layers[:int( len(model.layers) * (self.fine_tuning_rate.value / 100.0))]: layer.trainable = False else: # without transfer learning if self.architecture.value == "VGG16": model = self.getVGG16Model(IMG_WIDTH, IMG_HEIGHT, num_classes, False) elif self.architecture.value == "VGG19": model = self.getVGG19Model(IMG_WIDTH, IMG_HEIGHT, num_classes, False) elif self.architecture.value == "ResNet50": model = applications.ResNet50(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) for layer in model.layers: layer.trainable = True return model
def final_model(): img_width, img_height = 256, 256 model = applications.ResNet50(weights="imagenet", include_top=False, input_shape=(img_width, img_height, 3)) # Freeze the layers which you don't want to train. Here I am freezing the first 5 layers. for layer in model.layers[:5]: layer.trainable = False #Adding custom Layers x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) predictions = Dense(2, activation="softmax")(x) # creating the final model model_final = Model(inputs=model.input, output=predictions) # compile the model model_final.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=10, amsgrad=False), metrics=["accuracy"]) return model_final
def get_model(self): model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(self.img_width, self.img_height, 3)) for layer in model.layers: layer.trainable = False x = model.output x = Flatten()(x) x = Dropout(0.4)(x) x = Dense(self.dense_units, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(0.4)(x) x = Dense(self.dense_units, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(0.8)(x) predictions = Dense(10, activation='softmax')(x) model_final = Model(input=model.input, output=predictions) model_final.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy']) self.model = model_final self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model_final
def ResNet_mod(width, height, numLatitudes, numLongitudes, finalAct="softmax"): base_model = applications.ResNet50(weights="imagenet", include_top=False, input_shape=(width, height, 3)) x = base_model.output x = GlobalAveragePooling2D()(x) # x_la = Dense(128, activation='relu', name='fc1_1')(x) # x_la = Dense(1024, activation='relu', name='fc2_1')(x_la) x_la = Dense(numLatitudes)(x) latitudeBranch = Activation(finalAct, name="latitude_output")(x_la) # x_lo = Dense(128, activation='relu', name='fc1_2')(x) # x_lo = Dense(1024, activation='relu', name='fc2_2')(x_lo) x_lo = Dense(numLongitudes)(x) longitudeBranch = Activation(finalAct, name="longitude_output")(x_lo) model = Model( inputs=base_model.input, outputs=[latitudeBranch, longitudeBranch], name='posenet') for i,layer in enumerate(model.layers): print(i,layer.name) for layer in model.layers[:30]: layer.trainable = False return model
def _create_model(self): img_input = Input(shape=(self.img_rows, self.img_cols, self.color_depth), name='data') model = applications.ResNet50(include_top=False, weights=None, input_tensor=img_input, input_shape=(self.img_rows, self.img_cols, self.color_depth), pooling=None, classes=1000) # rename first layer because weights might have different shape model.get_layer(name='conv1').name = 'conv1_custom' x_newfc = AveragePooling2D((7, 7), name='avg_pool')(model.get_layer(name='activation_49').output) x_newfc = Flatten()(x_newfc) x_newfc = Dense(self.num_classes, activation='softmax', name='fc{}'.format(self.num_classes))(x_newfc) # Learning rate is changed to 0.001 model = Model(img_input, x_newfc) # first_idx = next(idx for idx, layer in enumerate(model.layers) if layer.name == 'activation_10') # last_idx = next(idx for idx, layer in enumerate(model.layers) if layer.name == 'activation_13') # # for layer in model.layers[0:-3]: # layer.trainable = False optimizer = SGD(lr=1e-3, decay=1e-7, momentum=0.9, nesterov=True) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) return model
def build(width, height, depth, classes): # model = Sequential() inputShape = (height, width, depth) # chanDim = -1 if K.image_data_format() == "channels_first": inputShape = (depth, height, width) # chanDim = 1 # model.add(Conv2D(64, (3, 3), padding="same", # input_shape=inputShape)) base_model = applications.ResNet50( weights="imagenet", include_top=False, input_shape=inputShape) # 预训练的VGG16网络,替换掉顶部网络 print('base_model', base_model.summary()) # for layer in base_model.layers[:15]: layer.trainable = False # 冻结预训练网络前15层 top_model = Sequential() # 自定义顶层网络 top_model.add( Flatten(input_shape=base_model.output_shape[1:])) # 将预训练网络展平 top_model.add(Dense(256, activation='relu')) # 全连接层,输入像素256 top_model.add(Dropout(0.5)) # Dropout概率0.5 top_model.add(Dense(classes, activation='softmax')) # 输出层,二分类 print('top_model', top_model.summary()) model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) return model
def save_bottlebeck_features(): datagen = ImageDataGenerator( preprocessing_function=preprocess_input_with_one_dim) print(full_path(un_dir['bottleneck_features_dir']) + 'border_data.npy') # build the VGG16 network model = applications.ResNet50(include_top=False, weights='imagenet', input_shape=(img_width, img_height, 3)) ''' generator = datagen.flow_from_directory( whole_shock_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_shock = model.predict_generator( generator, nb_shock_samples) np.save(open(full_path(un_dir['bottleneck_features_dir'])+'shock.npy', 'wb'), bottleneck_features_shock) ''' generator = datagen.flow_from_directory(whole_nonshock_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_nonshock = model.predict_generator( generator, nb_nonshock_samples) np.save( open( full_path(un_dir['bottleneck_features_dir']) + 'nonshock.npy', 'wb'), bottleneck_features_nonshock)