def _get_backbone_model(self): backbone_model = None if self.backbone_name == 'resnet18': backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet') if self.backbone_name == 'resnet34': backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'mobilenetv2': backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb0': backbone_model = EfficientNetB0(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb1': backbone_model = EfficientNetB1(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb3': backbone_model = EfficientNetB3(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'efficientnetb5': backbone_model = EfficientNetB5(input_shape=self.input_shape, include_top=False, weights='imagenet') elif self.backbone_name == 'densenet': backbone_model = DenseNet121(input_shape=self.input_shape, include_top=False, weights='imagenet') return backbone_model
def build_prepared_model(self): if self.config["model"].lower() == "mobilenetv2": base_model = MobileNetV2(input_shape=self.input_shape, weights=self.config['weights'], include_top=False) elif self.config["model"].lower() == "inceptionv3": base_model = InceptionV3(input_shape=self.input_shape, weights=self.config['weights'], include_top=False) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # add a fully-connected layer x = Dense(256, activation="relu")(x) x = Dropout(0.7)(x) # and a logistic layer predictions = Dense(self.num_classes, activation="softmax")(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in model.layers: layer.trainable = True adam_opt = optimizers.Adam( lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False, clipnorm=1.0, ) model.compile(loss="categorical_crossentropy", optimizer=adam_opt, metrics=["accuracy"]) log_and_print( f"{self.config['model']} model built as child model.\n Model summary:", self.config['logging'], ) print(model.summary()) return model
def __init__(self, model='vgg16', input_shape=(224, 224, 3)): self.input_shape = input_shape if model == 'vgg16': self.model = VGG16(input_shape=input_shape, weights='imagenet') elif model == 'mobilenet': self.model = MobileNetV2(input_shape=input_shape, weights='imagenet') elif model == 'resnet50': self.model = ResNet50(input_shape=input_shape, weights='imagenet') elif model == 'resnet152': self.model = ResNet152(input_shape=input_shape, weights='imagenet') elif model == 'inceptionresnet': self.model = InceptionResNetV2(input_shape=input_shape, weights='imagenet') else: raise ValueError( "The model you want to initialize as base is unknown.")
def loadCNN(wf_index): global get_output # mobilenet_output = MobileNetV2(input_shape=(img_channels, img_rows, img_cols), include_top=False, weights='imagenet', pooling='avg') model_input = Input(shape=(img_rows, img_cols, img_channels)) # mobilenet_output = MobileNetV2(input_shape=(img_rows, img_cols, img_channels), include_top=False, weights='imagenet', pooling='avg') # mobilenet_output = MobileNetV2(input_shape=(img_rows, img_cols, img_channels), input_tensor=model_input, include_top=False, weights='imagenet', pooling='avg') mobilenet_output = MobileNetV2(input_tensor=model_input, include_top=False, weights='imagenet', pooling='avg') print('mobilenet lastlayer output: {}'.format( mobilenet_output.layers[-1].output)) output = Dense(nb_classes, activation='softmax')(mobilenet_output.layers[-1].output) #sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model = Model(inputs=model_input, outputs=output) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) # Model summary model.summary() # Model conig details model.get_config() #from keras.utils import plot_model #plot_model(model, to_file='new_model.png', show_shapes = True) if wf_index >= 0: #Load pretrained weights fname = WeightFileName[int(wf_index)] print("loading ", fname) model.load_weights(fname) layer = model.layers[-1] get_output = K.function( [model.layers[0].input, K.learning_phase()], [ layer.output, ]) return model
def postPicture(request): if not (request.method == 'POST'): return badMethodJson try: try: data = json.loads(request.body) except: return JsonResponse( { 'msg': 'No post data in request', 'status': 400 }, status=400) img2 = Image.open(BytesIO(base64.b64decode(data['data']))) print('image opened') if not 'fast' in data: img2 = img2.resize((299, 299)) print('resized') model = Xception(weights='imagenet') print('Xception run successfully') else: img2 = img2.resize((224, 224)) print('resized') model = MobileNetV2(weights='imagenet') print('MobileNet run successfully') x = image.img_to_array(img2) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) outcome = decode_predictions(preds, top=1)[0][0] if outcome[2] < 0.35: outcome = "nothing recognised" else: outcome = outcome[1] return JsonResponse({'outcome': str(outcome)}) except Exception as err: print(err) return JsonResponse({'err': 'Error occured. Please try again'}, status='400') finally: subprocess.run('heroku restart --app ufluent', shell=True)
def get_model_actor_image(input_dims, output_dims): state_input = Input(shape=input_dims) oldpolicy_probs = Input(shape=( 1, output_dims, )) advantages = Input(shape=( 1, 1, )) rewards = Input(shape=( 1, 1, )) values = Input(shape=( 1, 1, )) feature_extractor = MobileNetV2(include_top=False, weights='imagenet') for layer in feature_extractor.layers: layer.trainable = False # Classification block x = Flatten(name='flatten')(feature_extractor(state_input)) x = Dense(1024, activation='relu', name='fc1')(x) out_actions = Dense(n_actions, activation='softmax', name='predictions')(x) model = Model( inputs=[state_input, oldpolicy_probs, advantages, rewards, values], outputs=[out_actions]) model.compile(optimizer=Adam(lr=1e-4), loss=[ ppo_loss(oldpolicy_probs=oldpolicy_probs, advantages=advantages, rewards=rewards, values=values) ]) model.summary() return model
def __init__(self, model='vgg16', input_shape=(224, 224, 3), weights='imagenet', include_top=True, conv_output=False): assert (conv_output and not include_top) or (not conv_output and include_top)\ or (not conv_output and not include_top), 'Either include_top or conv_output could be True, not both.' self.input_shape = input_shape self.include_top = include_top self.conv_output = conv_output self.last_conv_layer = -1 if model == 'vgg16': self.model = VGG16(input_shape=input_shape, weights=weights, include_top=include_top) self.last_conv_layer = -2 elif model == 'mobilenet': self.model = MobileNetV2(input_shape=input_shape, weights=weights, include_top=include_top) elif model == 'resnet50': self.model = ResNet50(input_shape=input_shape, weights=weights, include_top=include_top) elif model == 'resnet152': self.model = ResNet152(input_shape=input_shape, weights=weights, include_top=include_top) elif model == 'inceptionresnet': self.model = InceptionResNetV2(input_shape=input_shape, weights=weights, include_top=include_top) else: raise ValueError( "The model you want to initialize as base is unknown.") if weights is not None and weights is not 'imagenet': self.model.load_weights(weights, by_name=True)
def image_data_convert(file_list, network, resize_method, include_top): print('画像から特徴量を抽出中…') output_features = [] if network == 'nsfw': nsfw_model = predict.load_model('./nsfw.299x299.h5') if network == 'inception_resnet_v2': img_list = [] for j in tqdm(range(len(file_list))): img_file = file_list[j] orig_img = Image.open('./thumbnails/' + img_file + '.jpg') resized_img = resize_image(orig_img, 299, 299, resize_method) img_list.append(resized_img) img_list = np.array(img_list) inputs = Input(shape=(299, 299, 3)) model = InceptionResNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) output_features = model.predict(img_list) print("画像から特徴量への変換終了") elif network == 'mobilenet_v2': img_list = [] for j in tqdm(range(len(file_list))): img_file = file_list[j] orig_img = Image.open('./thumbnails/' + img_file + '.jpg') resized_img = resize_image(orig_img, 224, 224, resize_method) img_list.append(resized_img) img_list = np.array(img_list) inputs = Input(shape=(224, 224, 3)) model = MobileNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) #一度include_topをtrueにしてテスト output_features = model.predict(img_list) print("画像から特徴量への変換終了") else: return None final_out = [] for i in range(len(output_features)): final_out.append(output_features[i].flatten()) final_out = np.array(final_out) return final_out
def myfinetune(num_class, KerasModel, layer_num=-1): # Using the keras model or not if KerasModel: conv_model = MobileNetV2(weights=model_path, include_top=False, input_shape=(224, 224, 3)) conv_model.save_weights('model/base.h5') else: net = MyMobileNetV2() conv_model = net.MobileNetv2Conv() #conv_model = MobileNetv2Conv((224, 224, 3)) #conv_model.load_weights('model/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5') for i in range(144, 155): conv_model.layers.pop() x = conv_model.output channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 x = Conv2D(1280, (3, 3), padding='same', strides=(2, 2))(x) x = BatchNormalization(axis=channel_axis)(x) channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 x = Conv2D(640, (1, 1), padding='same', strides=(1, 1))(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) x = GlobalAveragePooling2D()(x) #x = Dense(512,activation='relu')(x) #x = Dropout(0.25)(x) x = Dense(256, activation='relu')(x) x = Dropout(0.5)(x) preds = Dense(num_class, activation='softmax')(x) model = Model(inputs=conv_model.input, outputs=preds) for i, layer in enumerate(model.layers): print(i, layer.name) for layer in model.layers[:layer_num]: layer.trainable = False for layer in model.layers[layer_num:]: layer.trainable = True return model
def predict(image1): model = MobileNetV2(include_top=True, weights='imagenet') model._make_predict_function() #model = load_model('tl_fine_tuning_InceptionResNetV2_120_breeds.h5') image = load_img(image1, target_size=(224, 224)) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((-1, image.shape[0], image.shape[1], 3)) / 255 #img = cv2.imread(image1) #img = cv2.resize(img, (299, 299)) #img = np.reshape(img, (-1, 299, 299, 3)) / 255 #with open('classes_encoding_120', 'rb') as f: # classes_labels = pickle.load(f) #label = classes_labels[model.predict_classes(image)[0]] predictions = model.predict(image) labels = decode_predictions(predictions) label = labels[0][0] label, probability = label[1], round(label[2]*100,2) return (label, probability)
def apply_pretrained_model(train_imgs, test_imgs): resnet = MobileNetV2(weights='imagenet', pooling=max, include_top=False, input_shape=train_imgs.shape[2:]) reshaped_imgs = np.reshape(train_imgs, (-1, ) + train_imgs.shape[2:]) reshaped_t_imgs = np.reshape(test_imgs, (-1, ) + test_imgs.shape[2:]) train_imgs_pp = preprocess_input(reshaped_imgs) test_imgs_pp = preprocess_input(reshaped_t_imgs) train_imgs_feat = resnet.predict(train_imgs_pp) test_imgs_feat = resnet.predict(test_imgs_pp) train_imgs_feat = np.reshape( train_imgs_feat, (train_imgs.shape[:2] + train_imgs_feat.shape[1:])) test_imgs_feat = np.reshape( test_imgs_feat, (test_imgs.shape[:2] + test_imgs_feat.shape[1:])) return train_imgs_feat, test_imgs_feat
def build(self): if self.base_model_name == MOBILENET_MODEL_NAME: self.base_model = MobileNetV2(input_shape=self.img_shape, include_top=False, weights="imagenet") elif self.base_model_name == RESNET_MODEL_NAME: self.base_model = ResNet50(input_shape=self.img_shape, include_top=False, weights="imagenet") x = self.base_model.output x = GlobalAveragePooling2D()(x) if self.range_mode: age_classes_number = age_ranges_number() age_output = Dense(units=age_classes_number, activation="softmax", name="age_output")(x) else: age_output = Dense(units=AGES_NUMBER, activation="softmax", name="age_output")(x) if not self.predict_gender: self.model = Model(inputs=self.base_model.input, outputs=age_output) else: gender_output = Dense(units=GENDERS_NUMBER, activation="softmax", name="gender_output")(x) self.model = Model(inputs=self.base_model.input, outputs=[age_output, gender_output])
def mobilenet(num_classes, embedding_size=84, load_from=None): """Pretrained mobilenetV2 model w/o last layer. Args: num_classes (int): Size of output layer. embedding_size (int): Embedding layer size [default: 84] load_from (str): Path to loadable weights [default: None]. Return: net: Ready-to-plug pretrained model. """ basenet = MobileNetV2(input_shape=(224, 224, 3), weights='imagenet', include_top=False, pooling='avg') basenet_output = basenet.output prediction = Dense(embedding_size, activation='sigmoid')(basenet_output) prediction = Dense(num_classes, activation='sigmoid')(prediction) net = Model(inputs=basenet.input, outputs=prediction) if load_from: net.load_weights(load_from) return net
def prediction_model(use_model, bs, imgs): if use_model == 'inceptionv3': base_model = InceptionV3(include_top=False, input_shape=(input_size, input_size, input_channels)) elif use_model == 'resnet50': base_model = ResNet50(include_top=False, input_shape=(input_size, input_size, input_channels)) elif use_model == 'vgg16': base_model = VGG16(include_top=False, input_shape=(input_size, input_size, input_channels)) elif use_model == 'mobilenet': base_model = MobileNet(include_top=False, input_shape=(input_size, input_size, input_channels)) elif use_model == 'mobilenetv2': base_model = MobileNetV2(include_top=False, input_shape=(input_size, input_size, input_channels)) elif use_model == 'vgg19': base_model = VGG19(include_top=False, input_shape=(input_size, input_size, input_channels)) model = Sequential() model.add(base_model) model.add(Flatten()) model.add(Dense(17, activation='sigmoid')) if use_model == 'inceptionv3': model.load_weights('inceptionv3.h5') elif use_model == 'resnet50': model.load_weights('resnet50.h5') elif use_model == 'vgg16': model.load_weights('vgg16.h5') elif use_model == 'mobilenet': model.load_weights('mobilenet.h5') elif use_model == 'mobilenetv2': model.load_weights('mobilenetv2.h5') elif use_model == 'vgg19': model.load_weights('vgg19.h5') resp = model.predict(np.array(imgs), batch_size = bs) resp_list = [] for img in resp: for label in list(img): resp_list.append(1 if label>0.2 else 0) return resp_list
def init_model(self, trainable=False): """ :param trainable: using pretrained model configuration or not :param model_conf: loaded from json configuration model scheme :return: """ model = MobileNetV2(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, alpha=self.ALPHA, weights='imagenet') for layer in model.layers: layer.trainable = trainable block = model.get_layer("block_16_project_BN").output x = Conv2D(112, padding="same", kernel_size=3, strides=1, activation="relu")(block) x = Conv2D(112, padding="same", kernel_size=3, strides=1, use_bias=False)(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = Conv2D(5, padding="same", kernel_size=1, activation="sigmoid")(x) model = Model(inputs=model.input, outputs=x) # divide by 2 since d/dweight learning_rate * weight^2 = 2 * learning_rate * weight # see https://arxiv.org/pdf/1711.05101.pdf regularizer = l2(self.WEIGHT_DECAY / 2) for weight in model.trainable_weights: with tf.keras.backend.name_scope("weight_regularizer"): model.add_loss(regularizer(weight)) self.MODEL = model
def image_data_convert_v2_save(file_list, network, resize_method, include_top): print('画像から特徴量を抽出中…') for j in tqdm(range(len(file_list))): img_file = file_list[j] save_path = './features/' + network + '/' + resize_method + '&' + str( include_top) + '&' + img_file + '.json' if os.path.exists(save_path): print(img_file + "の特徴量データは既存です") with open(save_path, mode='r', encoding='utf-8') as f: feature_size = len(json.load(f)) continue orig_img = Image.open('./thumbnails/' + img_file + '.jpg') if network == 'inception_resnet_v2': resized_img = resize_image(orig_img, 299, 299, resize_method) elif network == 'mobilenet_v2': resized_img = resize_image(orig_img, 224, 224, resize_method) else: return None img_array = np.array([resized_img]) if network == 'inception_resnet_v2': inputs = Input(shape=(299, 299, 3)) model = InceptionResNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) elif network == 'mobilenet_v2': inputs = Input(shape=(224, 224, 3)) model = MobileNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) else: return None output_feature = model.predict(img_array) output_feature_flatten = output_feature.flatten() feature_size = len(output_feature_flatten) with open(save_path, mode='w', encoding='utf-8') as f: json.dump(output_feature_flatten.tolist(), f, ensure_ascii=False, indent=4) print(img_file + "の特徴量をjson形式で保存しました。") return feature_size
def get_base_model(model_name, image_size): if model_name == 'vgg16': base_model = VGG16(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) if model_name == 'resnet50': base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) if model_name == 'xception': base_model = Xception(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) if model_name == 'densenet121': base_model = DenseNet121(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) if model_name == 'mobilenet0.75': base_model = MobileNet(include_top=False, weights='imagenet', alpha=0.75, input_shape=(image_size, image_size, 3)) if model_name == 'mobilenet1.0': base_model = MobileNet(include_top=False, weights='imagenet', alpha=1.0, input_shape=(image_size, image_size, 3)) if model_name == 'mobilenetv2': base_model = MobileNetV2(include_top=False, weights='imagenet', alpha=1.0, input_shape=(image_size, image_size, 3)) if model_name == 'inceptionv3': base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) if model_name == 'inceptionv2': base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3)) return base_model
def __init__(self): self.img_nrows = 224 self.img_ncols = 224 self.num = 1 self.base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(self.img_nrows, self.img_ncols, 3)) # self.base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(self.img_nrows, self.img_ncols, 3)) self.base_model._make_predict_function() self.content_layer = 'block_7_depthwise_relu' self.model_content = Model(inputs=self.base_model.input, outputs=self.base_model.get_layer(self.content_layer).output) self.model_content._make_predict_function() self.style_layers = ['block_3_depthwise_relu', 'block_5_depthwise_relu', 'block_7_depthwise_relu'] self.models_style = [] for layer in self.style_layers: self.models_style.append(Model(inputs=self.base_model.input, outputs=self.base_model.get_layer(layer).output)) self.models_style[-1]._make_predict_function()
def get_model(): input_tensor = Input(shape=(224, 224, 3)) base_model = MobileNetV2( input_shape=(224, 224, 3), include_top=False, weights="imagenet", input_tensor=input_tensor, ) x = base_model.output x = GlobalAveragePooling2D()(x) op = Dense(2, activation="softmax", name="final_output")(x) for layer in base_model.layers: layer.trainable = False model = Model(inputs=[input_tensor], outputs=[op]) model.summary() return model
def __init__(self, network="MobileNet"): self.network = network x, y = Data("../aiming/data").load() self.x_train, self.x_test, self.y_train, self.y_test = train_test_split( x, y, random_state=0, test_size=0.15, shuffle=True) self.y_train = to_categorical(self.y_train, num_classes=3) self.y_test = to_categorical(self.y_test, num_classes=3) if self.network == "MobileNet": self.model = MobileNetV2(input_shape=(96, 96, 3), weights=None, classes=3, alpha=0.5) elif self.network == "DenseNet": self.model = DenseNet121(input_shape=(96, 96, 3), weights=None, classes=3) sgd = SGD(lr=1e-3, momentum=0.9, nesterov=True) self.model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
def get_model(model_name, batch_size=1): if model_name == "resnet18_v1": import mxnet as mx from mxnet import gluon from mxnet.gluon.model_zoo import vision gluon_model = vision.get_model(model_name, pretrained=True) img_size = 224 data_shape = (batch_size, 3, img_size, img_size) net, params = relay.frontend.from_mxnet(gluon_model, {"data": data_shape}) return (net, params) elif model_name == "mobilenet_v2": import keras from keras.applications.mobilenet_v2 import MobileNetV2 keras.backend.clear_session( ) # Destroys the current TF graph and creates a new one. weights_url = "".join([ "https://github.com/JonathanCMitchell/", "mobilenet_v2_keras/releases/download/v1.1/", "mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.5_224.h5", ]) weights_file = "mobilenet_v2_weights.h5" weights_path = download_testdata(weights_url, weights_file, module="keras") keras_mobilenet_v2 = MobileNetV2(alpha=0.5, include_top=True, weights=None, input_shape=(224, 224, 3), classes=1000) keras_mobilenet_v2.load_weights(weights_path) img_size = 224 data_shape = (batch_size, 3, img_size, img_size) mod, params = relay.frontend.from_keras(keras_mobilenet_v2, {"input_1": data_shape}) return (mod, params)
def get_fe(fe, input_image): if fe == 'effnetb0': return EfficientNetB0(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'effnetb1': return EfficientNetB1(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'effnetb2': return EfficientNetB2(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'effnetb3': return EfficientNetB3(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'effnetb4': return EfficientNetB4(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'effnetb5': return EfficientNetB5(input_tensor=input_image, include_top=False), [ 'swish_last', 'block5_i_MB_swish_1', 'block3_i_MB_swish_1' ] elif fe == 'd53': return d53(input_image) elif fe == 'mnetv2': mnet = MobileNetV2(input_tensor=input_image, weights='imagenet') return mnet, [ 'out_relu', 'block_13_expand_relu', 'block_6_expand_relu' ] elif fe == 'mnet': mnet = MobileNet(input_tensor=input_image, weights='imagenet') return mnet, ['conv_pw_13_relu', 'conv_pw_11_relu', 'conv_pw_5_relu'] elif fe == 'r50': r50 = ResNet50(input_tensor=input_image, weights='imagenet') return r50, ['activation_49', 'activation_40', 'activation_22'] raise ValueError('Pls put the correct fe')
def build_model(in_w, in_h, classes): in_tensor = Input(shape=(in_w, in_h, 3)) base_model = MobileNetV2(include_top=False, weights='imagenet', input_tensor=in_tensor, input_shape=(in_w, in_h, 3), pooling='avg') for layer in base_model.layers: layer.trainable = False op = Dense(256, activation='relu')(base_model.output) op = Dropout(.25)(op) out_tensor = Dense(classes, activation='softmax')(op) model = Model(inputs=in_tensor, outputs=out_tensor) model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['categorical_accuracy']) return model
def build_actor(self, input_dims, output_dims, n_actions): state_input = Input(shape=input_dims) advantage = Input(shape=(1,)) old_prediction = Input(shape=(output_dims,)) feature_extractor = MobileNetV2(include_top=False, weights='imagenet') for layer in feature_extractor.layers: layer.trainable = False # Classification block x = Flatten(name='flatten')(feature_extractor(state_input)) x = Dense(1024, activation='relu', name='fc1')(x) out_actions = Dense(n_actions, activation='softmax', name='predictions')(x) model = Model(inputs=[state_input, old_prediction, advantage], outputs=[out_actions]) model.compile(optimizer=Adam(lr=1e-4), loss=[proximal_policy_optimization_loss( advantage=advantage, old_prediction=old_prediction)]) model.summary() return model
def __init__(self, model_type="ResNet50", weights="imagenet", input_shape=(128, 128)): """ """ model_types = ["MobileNet", "MobileNetV2", "VGG16", "ResNet50"] weights_types = ["imagenet", "random"] if weights not in weights_types: raise ValueError( "Invalid weights. Should be one of: {}".format(weights_types)) if model_type not in model_types: raise ValueError( "Invalid model type. Should be one of: {}".format(model_types)) 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) if model_type == "VGG16": self.model = VGG16(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "ResNet50": self.model = ResNet50(weights=weights, include_top=False, pooling='avg', input_shape=input_shape)
def build(self): base_model = MobileNetV2(input_tensor=Input(shape=(360,640,3)), \ alpha=.75, include_top=False, weights='imagenet', pooling='avg') #freeze_weights(base_model) #for layer in base_model.layers[:154]: # layer.trainable=False #for i,layer in enumerate(base_model.layers): # print(i,layer.name) x = base_model.output #testing x = Dense(200, activation='relu')(x) #x = Conv2D(8, (1, 1), padding='same')(x) #x = Flatten()(x) #x = Dropout(0.2, name='Dropout1')(x) x = Dense(self.num_outputs, activation='linear')(x) x = Reshape((self.num_outputs,))(x) model = Model(inputs=base_model.inputs, outputs=x) return model
def pretrained_model(self): from keras.applications.mobilenet_v2 import MobileNetV2 from keras.layers import Dense from keras.models import Model trained_model = MobileNetV2() trained_model.layers.pop() added = trained_model.layers[-1].output added = Dense(128, activation='relu')(added) pred = Dense(len(self.labellist), activation='softmax')(added) model = Model(input=trained_model.input, output=pred) for layer in trained_model.layers: layer.trainable = False model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.summary() return model
def build(self, **kwargs): input_layer = kwargs.get("input_layer") output_layer = kwargs.get("output_layer") if output_layer is None: return RuntimeError("Essential components missing.") if input_layer is None: input_layer = Input(shape=self.input_shape) base_model = MobileNetV2(input_tensor=input_layer, **self.comp_kwargs) for layer in base_model.layers: layer.trainable = False x = base_model.output x = GlobalAveragePooling2D()(x) op = output_layer(x) model = Model(inputs=[input_layer], outputs=[op]) return {"model": model}
def _create_model(self, num_classes): base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=NETWORK_INPUT_SHAPE) # for layer in base_model.layers: # layer.trainable = False x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(64, activation='relu')(x) x = Dropout(0.5)(x) predictions = Dense(num_classes, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # compile model model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=keras.optimizers.adam()) return model
def _build_model(self, input_layer, pretrained_model, output_layer, misc): input_tensor = Input(shape=( input_layer.get("image_height"), input_layer.get("image_width"), input_layer.get("color_channels"), )) base_model = MobileNetV2(input_tensor=input_tensor, **pretrained_model) x = base_model.output x = GlobalAveragePooling2D()(x) op = Dense(**output_layer)(x) for layer in base_model.layers: layer.trainable = False model = Model(inputs=[input_tensor], outputs=[op]) model.compile( optimizer=misc.get("optimizer"), loss=misc.get("loss"), metrics=misc.get("metrics"), ) return model