def __init__(self, input_size): input_image = Input(shape=(input_size, input_size, 3)) mobilenet = MobileNet(input_shape=(224,224,3), include_top=False) mobilenet.load_weights(MOBILENET_BACKEND_PATH) x = mobilenet(input_image) self.feature_extractor = Model(input_image, x)
def __init__(self, input_size, weights): input_image = Input(shape=(input_size, input_size, 3)) mobilenet = MobileNet(input_shape=(224,224,3), include_top=False) if weights: mobilenet.load_weights(weights) x = mobilenet(input_image) self.feature_extractor = Model(input_image, x)
def image_classification(paths): classification_dict = {} model = MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) for path in paths: image = mnetv2_loadimage(path) preds = model.predict(image) prediction = decode_predictions(preds, top=1)[0][0] img_class = prediction[1] img_confidence = prediction[2] if img_class in classification_dict: classification_dict[img_class].append((path, img_confidence)) else: classification_dict[img_class] = [(path, img_confidence)] return classification_dict
def imageModels(X, nb_classes, weights=None): # Note these all require exactly 3 input channels. from keras.applications import Xception, VGG16 from keras.applications.inception_v3 import InceptionV3 from keras.applications.nasnet import NASNetLarge, NASNetMobile from keras.applications.inception_resnet_v2 import InceptionResNetV2 from keras.utils.generic_utils import CustomObjectScope from keras.applications.mobilenet import MobileNet, DepthwiseConv2D weights = 'imagenet' # Could resize images to, e.g. 224 x 224 and then use weights='imagenet'. # Need to use --mels=224 --dur=2.6s with preprocess_data.py and --tile with train_network. input_shape = X.shape[1:] print("input_shape = ", input_shape) if False and ( 3 != input_shape[0] ): # then we're going to add a front end that gives us 3 channels front_end = Input(shape=input_shape) front_end = Conv2D(3, (3, 3), padding='valid', input_shape=input_shape, activation='relu')(front_end) input_shape = ( X.shape[1], X.shape[2], 3 ) # and now we'll set input_shape as the rest of the network wants else: front_end = Input(shape=input_shape) #base_model = NASNetMobile(input_shape=input_shape, weights=weights, include_top=False, input_tensor=front_end) with CustomObjectScope({ 'relu6': keras.applications.mobilenet.relu6, 'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D }): base_model = MobileNet(input_shape=input_shape, weights=weights, include_top=False, input_tensor=front_end, dropout=0.6) #base_model = Xception(input_shape=X[0].shape, weights=weights, include_top=False, input_tensor=front_end) top_model = Sequential() # top_model gets tacked on to pretrained model top_model.add(Flatten(input_shape=base_model.output_shape[1:])) top_model.add( Dense(1024)) # 128 is 'arbitrary' for now #ZZZ switch to 1024 top_model.add(Dense(nb_classes, name='FinalOutput')) # Final output layer #top_model.load_weights('bootlneck_fc_model.h5') model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) return model
def create_in(path_folders): model = MobileNet(weights='imagenet') img_list = [] folders = os.listdir(path_folders) # print(folders) for f in folders: dire = os.listdir(path_folders + f) for a in dire: caminho = '{}{}/{}'.format(path_folders, f, a) img_list.append(caminho) list_predicted = [] for index, im in enumerate(img_list): img = image.load_img(im, target_size=(224, 224)) x = mnetv2_input_from_image(img) preds = model.predict(x) ids = (np.fliplr(np.argsort(preds))[0][:5]).tolist() probs = [preds[0][i] for i in ids] list_predicted.append((im, ids, probs)) inv = dict() for x in id_to_name.dic.values(): inv[x] = [] for i, img in enumerate(list_predicted): for j, id_ in enumerate(list_predicted[i][1]): chave = id_to_name.dic[id_] inv[chave].append((img[0], list_predicted[i][2][j])) invs = dict() for v in inv: invs[v] = sorted(inv[v], key=lambda t: t[1], reverse=True) pickle.dump(invs, open("dictimgs.p", "wb"))
def mobilenet(classes = classes, imagesize = imagesize): mobilenet = MobileNet(include_top = False, input_shape = imagesize) for layer in mobilenet.layers[:-4]: layer.trainable = False model = Sequential() model.add(mobilenet) model.add(Flatten()) model.add(Dense(1024, activation = "relu")) model.add(Dropout(.5)) model.add(Dense(len(classes), activation = "softmax")) model.summary() return model
def print_shape(model_name, dim): model = None if model_name == 'mobile-net-v1': from keras.applications.mobilenet import MobileNet model = MobileNet(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'mobile-net-v2': from keras.applications.mobilenet_v2 import MobileNetV2 model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'vgg-16': from keras.applications.vgg16 import VGG16 model = VGG16(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'vgg-19': from keras.applications.vgg19 import VGG19 model = VGG19(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'densenet-121': from keras.applications.densenet import DenseNet121 model = DenseNet121(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'densenet-169': from keras.applications.densenet import DenseNet169 model = DenseNet169(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'densenet-201': from keras.applications.densenet import DenseNet201 model = DenseNet201(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'resnet-50': from keras.applications.resnet50 import ResNet50 model = ResNet50(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'xception': from keras.applications.xception import Xception model = Xception(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'inception-v3': from keras.applications.inception_v3 import InceptionV3 model = InceptionV3(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) elif model_name == 'inceptionresnet-v2': from keras.applications.inception_resnet_v2 import InceptionResNetV2 model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) path = os.path.join('bin', os.path.join('shapes', '{}-{}.jpg')) plot_model(model, to_file=path.format(model_name, dim), show_shapes=True, show_layer_names=True, rankdir='TB')
def createModileNetModel(self, image_size, num_of_layers): self.model = MobileNet(input_shape=(image_size, image_size, num_of_layers), alpha=1.0, include_top=False) for layer in self.model.layers: layer.trainable = False # Add new top layer which is a conv layer of the same size as the previous layer so that only 4 coords of BBox can be output x = self.model.layers[-1].output x = Conv2D(4, kernel_size=4, name="coords")(x) # In the line above kernel size should be 3 for img size 96, 4 for img size 128, 5 for img size 160 etc. x = Reshape( (4, ))(x) # These are the 4 predicted coordinates of one BBox self.model = Model(inputs=self.model.input, outputs=x)
def mySegNet(input_shape): base_model = MobileNet(input_shape=(224,224,3), include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) cnn_model = Model(inputs=base_model.input, outputs=x) model = Sequential(); model.add(TimeDistributed(cnn_model, input_shape=input_shape)) model.add(TimeDistributed(Flatten())) model.add(LSTM(200, return_sequences=True)) model.compile(optimizer='adam', loss='mean_squared_error') #print(model.summary()) return model
def make_classifier(optimizer): restnet = MobileNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) output = restnet.layers[-1].output output = keras.layers.Flatten()(output) restnet = Model(restnet.input, output=output) for layer in restnet.layers: layer.trainable = True restnet.summary() model = Sequential() model.add(restnet) model.add(Dense(512, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(13, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy']) return model
def __init__(self, input_shape=(112, 112, 3), num_clases): mobil = MobileNet(weights="imagenet", include_top=False, input_shape=input_shape) #almaceno la salida x = mobil.output #aplico transferlearning for layer in mobil.layer: layer.trainable = False x = Dense(num_clases, activation='sigmoid', name='predictions')(mobil.layers[-2].output) #creo el modelo self.__my_mobil = Model(input=mobil.input, output=x)
def mobile_net(class_num=6): img_input = Input(shape=(None, None, 3), name="image") base_model = MobileNet(input_tensor=img_input, pooling='max', include_top=False) x = base_model.output x = Dense(64)(x) x = Activation('relu')(x) x = Dropout(0.5)(x) x = Dense(32)(x) x = Activation('relu')(x) class_out = Dense(class_num, activation='sigmoid')(x) model = Model(img_input, class_out, name='ResNet') return model
def test_convert_keras_to_pmml(): print("--- Test Keras to PMML ---") keras_model = VGG16() class_map = load_imagenet_classes() pmml = convert(keras_model, class_map=class_map, description="VGG-16 ImageNet Model") pmml.save_pmml(VGG_16_MODEL) keras_model = ResNet50() pmml = convert(keras_model, class_map=class_map, description="ResNet50 ImageNet Model") pmml.save_pmml(RESNET50_MODEL) keras_model = MobileNet() pmml = convert(keras_model, class_map=class_map, description="MobileNet ImageNet Model") pmml.save_pmml(MOBILENET_PATH)
def build_model(lr=0.002): model_pre = MobileNet(input_shape=(224, 224, 3), weights=None, include_top=False) #model_pre.load_weights("G:/keras_weights/mobilenet_1_0_224_tf_no_top.h5") x = model_pre.output #x = model_pre.get_layer("conv_pw_5_relu").output x = Reshape((1, 1, -1))(x) x = Conv2D(32,(1,1),strides=(1, 1), padding="same")(x) #x = Flatten()(x) #x = Activation('relu')(x) y = Reshape((32,))(x) #y = Dense(32, activation='relu')(x) model = Model(model_pre.input,y) model.compile(Adam(lr),loss='mean_squared_error') return model
def create_mobile_net_model(size, alpha): model_net = MobileNet(input_shape=(size, size, 3), include_top=False, alpha=alpha) x = _depthwise_conv_block(model_net.layers[-1].output, 1024, alpha, 1, block_id=14) x = Conv2D(64, (3, 3), activation='relu')(x) x = MaxPooling2D(pool_size=(4, 4))(x) x = Conv2D(8, kernel_size=(1, 1), padding="same")(x) x = Reshape((8, ))(x) return Model(inputs=model_net.input, outputs=x)
class MobileNet_wrapper(object): def __init__(self): self.model = MobileNet() self.name = "MobileNet" self.input_size = (224, 224, 3) self.num_classes = 1000 def predict(self, images, top=None): if top is None: top = self.num_classes images = preprocess_input(images) predictions = self.model.predict(images) labels = decode_predictions(predictions, top=top) return labels
def modelSelect(self, var): if (var == '1'): self.model = ResNet50(weights='imagenet', pooling=max, include_top=False) elif (var == '2'): self.model = VGG19(weights='imagenet', pooling=max, include_top=False) elif (var == '3'): self.model = MobileNet(weights='imagenet', pooling=max, include_top=False)
def mobilenet_retinanet(num_classes, backbone='mobilenet224_1.0', inputs=None, modifier=None, **kwargs): alpha = float(backbone.split('_')[1]) # choose default input if inputs is None: inputs = keras.layers.Input((None, None, 3)) mobilenet = MobileNet(input_tensor=inputs, alpha=alpha, include_top=False, pooling=None, weights=None) # get last layer from each depthwise convolution blocks 3, 5, 11 and 13 outputs = [mobilenet.get_layer(name='conv_pw_{}_relu'.format(i)).output for i in [3, 5, 11, 13]] # create the mobilenet backbone mobilenet = keras.models.Model(inputs=inputs, outputs=outputs, name=mobilenet.name) # invoke modifier if given if modifier: mobilenet = modifier(mobilenet) # create the full model model = retinanet.retinanet_bbox(inputs=inputs, num_classes=num_classes, backbone=mobilenet, **kwargs) return model
def getModel(self): mobilenet = MobileNet(input_shape=(224, 224, 3), include_top=False, weights='imagenet') #using mobilenet as backbone last_layer = mobilenet.get_layer(index=-1).output #get the last layer for layer in mobilenet.layers: layer.trainable = True # set all mobilenet layers trainable to True c1 = Conv2D(filters=1024, kernel_size=(3, 3))(last_layer) l1 = LeakyReLU(alpha=0.3)(c1) d1 = Dropout(0.3)(l1) c2 = Conv2D(filters=512, kernel_size=(3, 3))(d1) l2 = LeakyReLU(alpha=0.6)(c2) d2 = Dropout(0.5)(l2) f1 = Flatten()(d2) dense1 = Dense(24)(f1) o = LeakyReLU(alpha=0.3)(dense1) return Model(inputs=mobilenet.input, outputs=o)
def create_model(): if model_name == "mobilenet": base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224, 224, 3)), input_shape=(224, 224, 3)) #base_model.summary() model = Model(input=base_model.input, output=base_model.layers[-1].output) else: base_model = None return model
def predict(): args = parse_args() model_name = args.model_name dataset_path = args.dataset_path batch_size = 32 # Load model and get logits Model, preprocess_input, size = get_model_artifacts(model_name) idg = ImageDataGenerator(preprocessing_function=preprocess_input) model = Model(weights="imagenet", classifier_activation=None) flow = idg.flow_from_directory(dataset_path, shuffle=False, target_size=(size, size), batch_size=batch_size) # Generate predictions predictions = [] n_batches = 0 for x, y in tqdm(flow): preds = model.predict(x, verbose=0) predictions.append(preds) n_batches += 1 if n_batches >= math.ceil(len(flow.filenames) / batch_size): # Manually break the loop as the generator loops indefinitely break predictions = np.concatenate(predictions)[:len(flow.filenames)] # Calculate accuracy for double checking targets = np.array( list(map(lambda x: to_int(x.split("/")[0]), flow.filenames))) y = predictions.argmax(axis=1) print(f"Accuracy = {np.mean(targets==y)}") # Save paths and predictions into an npz file dataset_dir_name = os.path.split(dataset_path)[1] output_filename = f"soft_targets-{model_name}-{dataset_dir_name}.npz" output_path = os.path.join(os.path.split(dataset_path)[0], output_filename) np.savez(output_path, paths=flow.filenames, preds=predictions) print(f"Results saved in '{output_path}'")
def yolo_body_mobilenet(inputs, num_anchors, num_classes): """Create YOLO_V3_mobilenet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs,weights='imagenet') # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x 1024 # conv_pw_11_relu :26 x 26 x 512 # conv_pw_5_relu : 52 x 52 x 256 # mobilenet.summary() f1 = mobilenet.get_layer('conv_pw_13_relu').output # f1 :13 x 13 x 1024 # spp # sp3 = MaxPooling2D(pool_size=(3,3),strides=1,padding='same')(f1) # sp5 = MaxPooling2D(pool_size=(5,5),strides=1,padding='same')(f1) # f1 = compose( # Concatenate(), # DarknetConv2D_BN_Leaky(512, (1,1)))([sp3,sp5,f1]) # end f1 = DarknetSeparableConv2D_BN_Leaky(256,(3,3))(f1) y1 = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(f1) f1 = compose( DarknetConv2D_BN_Leaky(128, (1,1)), UpSampling2D(2))(f1) f2 = mobilenet.get_layer('conv_pw_11_relu').output # f2: 26 x 26 x 512 f2 = compose( Concatenate(), DarknetSeparableConv2D_BN_Leaky(256,(3,3)) )([f1,f2]) y2 = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(f2) return Model(inputs = inputs, outputs=[y1,y2])
class VisualFeaturesExtractor(object): """ """ def __init__(self, model_type="MobileNet", weights="imagenet", input_shape=(128, 128, 3)): """ """ model_types = ["MobileNet", "MobileNetV2"] if model_type not in model_types: raise ValueError("Invalid model type") assert input_shape[0] > 32 assert input_shape[1] > 32 self.input_shape = input_shape if model_type == "MobileNet": self.model = MobileNet(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "MobileNetV2": self.model = MobileNetV2(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) def extract(self, frame, detection=None): """ """ if detection is not None: x = detection[0] y = detection[1] w = detection[2] h = detection[3] crop_frame = frame[y:y + h, x:x + w] else: crop_frame = frame frame_resized = cv2.resize(crop_frame, (self.input_shape[0], self.input_shape[1])) temp = frame_resized[0] frame_resized[0] = frame_resized[3] frame_resized[3] = temp x = image.img_to_array(frame_resized) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = self.model.predict(x)[0] return features
def build_model(self, args=None, for_training=True): input_shape = (224, 224, 3) input_tensor = Input(input_shape) num_classes = 9 ############################################################################################################################### weights = None if for_training and not args.pretrained else 'imagenet' mobnet_basic = MobileNet(weights=weights, include_top=False, input_shape=input_shape, input_tensor=input_tensor) if for_training and args.train_dense_only: # Disable training for the convolutional layers for index, layer in enumerate(mobnet_basic.layers): layer.trainable = False # TODO Remove code below if unnecessary # Disable the training for some layers of mobilenet # if index < 89: # mobnet_basic.layers[index].trainable = False # print("{}#{}, trainable={}".format(index, layer.name, layer.trainable)) # layer.trainable = False # Extend mobile net by own fully connected layer x = mobnet_basic.layers[-1].output # TODO Evaluate if the original layers from MobileNet bring better performance/accuracy x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 1024), name='reshape_1')(x) x = Dropout(0.5, name='dropout')(x) x = Conv2D(num_classes, (1, 1), padding='same', name='conv_preds')(x) x = Activation('softmax', name='act_softmax')(x) predictions = Flatten()(x) # x = Flatten()(x) # predictions = Dense(num_classes, activation='softmax', name='predictions')(x) mobnet_extended = Model(inputs=input_tensor, outputs=predictions, name='mobilenet_cls') # Finalize the model by compiling it if for_training: mobnet_extended.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=args.learning_rate, decay=args.decay_rate)) return mobnet_extended
def build_model(self, args=None, for_training=True): input_shape = (224, 224, 3) input_tensor = Input(input_shape) weights = None if for_training and not args.pretrained else 'imagenet' mobnet_basic = MobileNet(weights=weights, include_top=False, input_shape=input_shape, input_tensor=input_tensor) if for_training and args.train_dense_only: # Disable training for the convolutional layers for index, layer in enumerate(mobnet_basic.layers): layer.trainable = False reg = l1(args.regularize) if for_training else l1(0.0) dropout = args.dropout if for_training else 0.5 # Extend mobilenet by own fully connected layer x = mobnet_basic.layers[-1].output x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 1024), name='reshape_1')(x) x = Dropout(dropout, name='dropout')(x) x = Conv2D(49, (1, 1), padding='same', name='pre_predictions', activation='relu', kernel_regularizer=reg, bias_regularizer=reg)(x) x = Conv2D(1, (1, 1), padding='same', name='predictions', activation='linear', kernel_regularizer=reg, bias_regularizer=reg)(x) predictions = Flatten()(x) mobnet_extended = Model(inputs=input_tensor, outputs=predictions, name='mobilenet_reg') # Finalize the model by compiling it if for_training: mobnet_extended.compile(loss='mae', metrics=['mse'], optimizer=Adam(lr=args.learning_rate, decay=args.decay_rate)) return mobnet_extended
class MLclassifer: def __init__(self): num_cores = 4 num_CPU = 1 num_GPU = 0 config = tf.ConfigProto(inter_op_parallelism_threads=num_cores, allow_soft_placement=True, device_count={ 'CPU': num_CPU, 'GPU': num_GPU }) session = tf.Session(config=config) K.set_session(session) r = 224 a = 0.5 self.model = MobileNet(include_top=True, weights='imagenet', input_shape=(r, r, 3), alpha=a) def classifyPath(self, name): img = image.load_img(name, target_size=(224, 224)) self.classify(self.prepare_image(img)) def prepare_image(self, img): if img.mode != "RGB": img = img.convert("RGB") img = img.resize((224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) return x def classify(self, x): x = preprocess_input(x) features = self.model.predict(x) tags = [] for item in decode_predictions(features, 10)[0]: print((item[1] + ": ")[0:20] + str(item[2] * 100)[0:5] + "%") if (item[2] > 0.005): tags.append(item[1] + ": " + str(item[2] * 100).split(".")[0] + "%") return tags
def get_model(model_name, shape, n_classes): if model_name == 'xception': base_model = Xception(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 elif model_name == 'incres': base_model = InceptionResNetV2(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .2 elif model_name == 'inception': base_model = InceptionV3(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 elif model_name == 'resnet': base_model = ResNet50(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 elif model_name == 'mobilenet': base_model = MobileNet(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 elif model_name == 'nasnet': base_model = NASNetMobile(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 elif model_name == 'densenet': base_model = DenseNet121(include_top=False, input_shape=(shape, shape, 3), pooling='avg') drop = .1 else: raise ValueError('Network name is unknown') x = base_model.output x = Dropout(drop)(x) predictions = Dense(n_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = False return model, preprocess_input
def __init__(self, model="mobilenet", weights="imagenet", include_top=True): if model_name == "vgg16": self.base_model = VGG16(weights=weights) self.model = Model(input=self.base_model.input, output=self.base_model.get_layer('fc1').output) self.image_size = (224, 224) elif model_name == "vgg19": self.base_model = VGG19(weights=weights) self.model = Model(input=self.base_model.input, output=self.base_model.get_layer('fc1').output) self.image_size = (224, 224) elif model_name == "resnet50": self.base_model = ResNet50(weights=weights) self.base_model.summary() self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('fc1000').output) self.image_size = (224, 224) elif model_name == "inceptionv3": self.base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) self.model = Model(input=self.base_model.input, output=self.base_model.get_layer('custom').output) self.image_size = (299, 299) elif model_name == "inceptionresnetv2": self.base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('custom').output) self.image_size = (299, 299) elif model_name == "mobilenet": self.base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('conv_pw_13_relu').output) self.image_size = (224, 224) elif model_name == "mobilenetv2": self.base_model = MobileNeV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) self.base_model.summary() self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('conv_pw_13_relu').output) self.image_size = (224, 224) elif model_name == "xception": self.base_model = Xception(weights=weights, , input_tensor=Input(shape=(299,299,3))) self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('avg_pool').output) self.image_size = (299, 299) elif model_name == "densenet": self.base_model = DenseNet121(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('avg_pool').output) self.image_size = (224, 224) elif model_name == "xception": self.base_model = NASNetMobile(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) self.model = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer('avg_pool').output) self.image_size = (224, 224) else: self.base_model = None self.features = [] self.labels = []
def get_3d_cnn_model_image_audio(): input_A = Input(shape=(4, 108, 192, 3)) image_model = Convolution3D(filters=32, kernel_size=(3, 3, 3), activation='relu', padding='same', data_format='channels_last')(input_A) image_model = MaxPooling3D(pool_size=(2, 2, 2))(image_model) image_model = Flatten()(image_model) image_model = Dense(32)(image_model) image_model = BatchNormalization()(image_model) image_model = Activation('relu')(image_model) input_B = Input(shape=(128, 345, 1)) audio_model = MobileNet(input_shape=(128, 345, 1), alpha=0.25, depth_multiplier=1, dropout=1e-3, include_top=False, weights=None, input_tensor=None, pooling='avg')(input_B) audio_model = Dense(32)(audio_model) audio_model = BatchNormalization()(audio_model) audio_model = Activation('relu')(audio_model) model = Concatenate()([image_model, audio_model]) model = Dense(1, activation='sigmoid')(model) model = Model(inputs=[input_A, input_B], outputs=model) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print_summary(model, line_length=120) return model
def createMobileNet(): mobileNet = MobileNet(include_top=False, weights='imagenet', input_shape=input_shape) output = mobileNet.layers[-1].output output = keras.layers.Flatten()(output) ModelmobileNet = Model( inputs=mobileNet.input, outputs=output) # base_model.get_layer('custom').output) ModelmobileNet.trainable = False for layer in ModelmobileNet.layers: layer.trainable = False return ModelmobileNet
def create_model(): base_mobilenet_model = MobileNet(input_shape=[128, 128, 1], include_top=False, weights=None) multi_disease_model = Sequential() multi_disease_model.add(base_mobilenet_model) multi_disease_model.add(GlobalAveragePooling2D()) multi_disease_model.add(Dropout(0.5)) multi_disease_model.add(Dense(512)) multi_disease_model.add(Dropout(0.5)) multi_disease_model.add(Dense(13, activation='sigmoid')) multi_disease_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy', 'mae']) return multi_disease_model
def __init__(self, model_type="MobileNet", weights="imagenet", input_shape=(128, 128, 3)): """ """ model_types = ["MobileNet", "MobileNetV2"] if model_type not in model_types: raise ValueError("Invalid model type") assert input_shape[0] > 32 assert input_shape[1] > 32 self.input_shape = input_shape if model_type == "MobileNet": self.model = MobileNet(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "MobileNetV2": self.model = MobileNetV2(weights=weights, include_top=False, pooling='avg', input_shape=input_shape)
def imagenet_mobilenet(input_shape, num_classes): mn = MobileNet(input_shape=input_shape, include_top=False, weights='imagenet') n = len(mn.layers) print('n=%d' % n) for i in range(int(n * .8)): mn.layers[i].trainable = False model = Sequential() model.add(mn) model.add(Flatten()) model.add(Dense(num_classes)) model.add(Activation('softmax')) n = model.layers[0].layers return model
def test_template_dl_keras(self): self.assertEqual(restapi_version(), "0.1.1237") temp = get_temp_folder(__file__, "temp_template_dl_keras") from keras.applications.mobilenet import MobileNet # pylint: disable=E0401,E0611 model = MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) model_name = os.path.join(temp, "model.keras") model.save(model_name) img_input = os.path.join(temp, "..", "data", "wiki_modified2.png") img_input = numpy.array(Image.open(img_input)) mo = restapi_load({'model': model_name}) pred = restapi_predict(mo, img_input) self.assertIsInstance(pred, numpy.ndarray) self.assertEqual(pred.shape, (1, 1000))
class MobileNetWrapper(object): def __init__(self): logger.info('Loading MobileNet') self.model = MobileNet(weights='imagenet') def predict(self, img): """ # Arguments img: a numpy array # Returns A dict containing predictions """ img = Image.fromarray(img) img = img.resize((224, 224)) x = keras_image.img_to_array(img)[:, :, :3] x = np.expand_dims(x, axis=0) x = preprocess_input_mobilenet(x) features = self.model.predict(x) predictions = decode_predictions_mobilenet(features)[0] clean_predictions = [{'score': str(k), 'class': j} for (i, j, k) in predictions] return json.dumps(clean_predictions)
model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) model = Model(input=base_model.input, output=base_model.get_layer('custom').output) image_size = (299, 299) elif model_name == "inceptionresnetv2": base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) model = Model(input=base_model.input, output=base_model.get_layer('custom').output) image_size = (299, 299) elif model_name == "mobilenet": base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) model = Model(input=base_model.input, output=base_model.get_layer('custom').output) image_size = (224, 224) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None # get all the train labels train_labels = os.listdir(train_path) # get all the test images paths test_images = os.listdir(test_path)
Keras的applications模块中就提供了带有预训练权重的深度学习模型。 该模块会根据参数设置,自动检查本地的~/.keras/models/目录下是否含有所需要 的权重,没有时会自动下载,在notebook上下载会占用notebook线程资源,不太方便, 因此也可以手动wget。 """ #以mobilenet为例,finetune的过程 import tensorflow as tf from keras.optimizers import SGD from keras.callbacks import ModelCheckpoint, TensorBoard from keras.applications.mobilenet import MobileNet from keras.layers import Input, Reshape, AvgPool2D, Dropout, \ Conv2D, BatchNormalization, Activation from keras.models import Model #加载预训练权重,输入大小可以设定,include_top表示是否包括顶层的全连接层 base_model = MobileNet(input_shape= (128, 128, 3), include_top = False) #添加新层,get_layer方法可以根据层名返回该层,output用于返回该层的输出张量tensor with tf.name_scope('output'): x = base_model.get_layer('conv_dw6_relu').output x = Conv2D(256, kernel_size=(3,3))(x) x = Activation('relu')(x) x = AvgPool2D(pool_size = (5,5))(x) x = Dropout(rate = 0.5)(x) x = Conv2D(10, kernel_size = (1,1))(x) predictions = Reshape((10,))(x) #finetune模型 model = Model(inputs= base_model.input, outputs= predictions)
def __init__(self): logger.info('Loading MobileNet') self.model = MobileNet(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) model = Model(input=base_model.input, output=base_model.get_layer('batch_normalization_1').output) image_size = (299, 299) elif model_name == "inceptionresnetv2": base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3))) model = Model(input=base_model.input, output=base_model.get_layer('batch_normalization_1').output) image_size = (299, 299) elif model_name == "mobilenet": base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3)) model = Model(input=base_model.input, output=base_model.get_layer('batch_normalization_1').output) image_size = (224, 224) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None print ("[INFO] successfully loaded base model and model...") # path to training dataset train_labels = os.listdir(train_path) # encode the labels