def model_inceptionresnet_pooled(input_shape=(None, None, 3), indexes=list(range(43)), pool_size=(5, 5), name='', return_sizes=False): """ Returns the wide MLSP features, spatially pooled, from InceptionResNetV2. * input_shape: shape of the input images * indexes: indices of the modules to use * pool_size: spatial extend of the MLSP features * name: name of the model * return_sizes: return the sizes of each layer: (model, pool_sizes) :return: model or (model, pool_sizes) """ print('Loading InceptionResNetV2 multi-pooled with input_shape:', input_shape) model_base = InceptionResNetV2(weights = 'imagenet', include_top = False, input_shape = input_shape) print('Creating multi-pooled model') ImageResizer = Lambda(lambda x: tf.image.resize_area(x, pool_size), name='feature_resizer') feature_layers = [l for l in model_base.layers if 'mixed' in l.name] feature_layers = [feature_layers[i] for i in indexes] pools = [ImageResizer(l.output) for l in feature_layers] conc_pools = Concatenate(name='conc_pools', axis=3)(pools) model = Model(inputs = model_base.input, outputs = conc_pools) if name: model.name = name if return_sizes: pool_sizes = [[np.int32(x) for x in f.get_shape()[1:]] for f in pools] return model, pool_sizes else: return model
def save(self, *args, **kwargs): try: img = load_img(self.picture, target_size=(299,299)) img_array = img_to_array(img) ## model takes 4D array bcoz it can use multiple images ## but our is only 1 so 1 at the beginning ## we need to convert our 3D array to 4d array (299, 299, 3) -> (1, 299, 299, 3) ## we can do so by using numpy to_pred = np.expand_dims(img_array, axis=0) #(1,299,299, 3) # preprocess input preprocess = preprocess_input(to_pred) ## assign model model = InceptionResNetV2(weights='imagenet') prediction = model.predict(preprocess) ## returns large array of 0,0...some number # this decodes the prediction decoded = decode_predictions(prediction)[0][0] thingName = decoded[1] probab = decoded[2] #print(decoded) #print(thingName) #print(probab) self.classification = thingName self.probability = probab print('success') except Exception as e: print('classification failed', e) super().save(*args, **kwargs)
def pretrained_model2(): from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 pretrained_model2 = InceptionResNetV2(include_top=False, input_shape=(224, 224, 3), weights='imagenet', layers=tf.keras.layers) for layer in pretrained_model2.layers[:-280]: layer.trainable = False model2 = Sequential() model2.add(layers.AveragePooling2D((2, 2), name='avg_pool')) model2.add(layers.Flatten()) model2.add(layers.Dense(256, activation='relu')) model2.add(layers.Dropout(0.5)) model2.add(layers.Dense(64, activation='relu')) model2.add(layers.Dropout(0.3)) model2.add(layers.Dense(4, activation='softmax')) preinput2 = pretrained_model2.input preoutput2 = pretrained_model2.output output2 = model2(preoutput2) model2 = Model(preinput2, output2) model2.summary() return model2
def model_inceptionresnet_multigap(input_shape=(224, 224, 3), return_sizes=False): """ Build InceptionResNetV2 multi-GAP model, that extracts narrow MLSP features. * input_shape: shape of the input images * return_sizes: return the sizes of each layer: (model, gap_sizes) :return: model or (model, gap_sizes) """ print('Loading InceptionResNetV2 multi-gap with input_shape:', input_shape) model_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=input_shape) print('Creating multi-GAP model') feature_layers = [l for l in model_base.layers if 'mixed' in l.name] gaps = [GlobalAveragePooling2D(name="gap%d" % i)(l.output) for i, l in enumerate(feature_layers)] concat_gaps = Concatenate(name='concatenated_gaps')(gaps) model = Model(inputs=model_base.input, outputs=concat_gaps) if return_sizes: gap_sizes = [np.int32(g.get_shape()[1]) for g in gaps] return (model, gap_sizes) else: return model
def loadPretrainedWeights(): pretrained_weights={} pretrained_weights['vgg16']=VGG16(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['vgg19']=VGG19(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['resnet50']=ResNet50(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inceptionv3']=InceptionV3(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inception-resentv2']=InceptionResNetV2(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['xception']=Xception(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet121']=DenseNet121(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet169']=DenseNet169(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet201']=DenseNet201(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['mobilenet']=MobileNet(weights='imagenet', include_top=False,pooling='avg') #N retrained_weights['nasnetlarge']=NASNetLarge(weights='imagenet', include_top=False,pooling='avg',input_shape = (224, 224, 3)) #N pretrained_weights['nasnetmobile']=NASNetMobile(weights='imagenet', include_top=False,pooling='avg') #N pretrained_weights['mobilenetV2']=MobileNetV2(weights='imagenet', include_top=False,pooling='avg') return pretrained_weights
def create_model(): """Create image classification object""" file_name = "./resnet50.h5" dir_path = os.path.dirname(os.path.realpath(__file__)) full_path = os.path.join(dir_path, file_name) if os.path.exists(full_path): model = load_model(full_path, custom_objects=None, compile=True) else: model = InceptionResNetV2( include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, ) save_model(model, full_path, overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None) data = {"model": model} return data
def get_model(): base_model = InceptionResNetV2(weights='imagenet', include_top=False, pooling='avg') x = base_model.output y_pred = Dense(4, activation='sigmoid')(x) return Model(inputs=base_model.input, outputs=y_pred)
def get_base_model(model_name, weights_path, weight_decay=1e-4): """ Define base model used in transfer learning. """ if not weights_path: weights_path = 'imagenet' if model_name == 'VGG16': base_model = VGG16(weights=weights_path, include_top=False) elif model_name == 'VGG19': base_model = VGG19(weights=weights_path, include_top=False) elif model_name == 'ResNet50V1': base_model = awsdet.models.backbones.ResNet50(weights=None, include_top=False, weight_decay=weight_decay) elif model_name == 'ResNet50V2': base_model = awsdet.models.backbones.ResNet50V2(weights=None, include_top=False, weight_decay=weight_decay) elif model_name == 'Xception': base_model = Xception(weights=weights_path, include_top=False) elif model_name == 'InceptionV3': base_model = InceptionV3(weights=weights_path, include_top=False) elif model_name == 'InceptionResNetV2': base_model = InceptionResNetV2(weights=weights_path, include_top=False) elif model_name == 'MobileNet': base_model = MobileNet(weights=weights_path, include_top=False) else: raise ValueError( 'Valid base model values are: "VGG16","VGG19","ResNet50V1", "ResNet50V2", "Xception", \ "InceptionV3","InceptionResNetV2","MobileNet".' ) return base_model
def build_model(): base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channel), pooling='avg') image_input = base_model.input x = base_model.layers[-1].output out = Dense(embedding_size)(x) image_embedder = Model(image_input, out) input_a = Input((img_size, img_size, channel), name='anchor') input_p = Input((img_size, img_size, channel), name='positive') input_n = Input((img_size, img_size, channel), name='negative') normalize = Lambda(lambda x: K.l2_normalize(x, axis=-1), name='normalize') x = image_embedder(input_a) output_a = normalize(x) x = image_embedder(input_p) output_p = normalize(x) x = image_embedder(input_n) output_n = normalize(x) merged_vector = concatenate([output_a, output_p, output_n], axis=-1) model = Model(inputs=[input_a, input_p, input_n], outputs=merged_vector) return model
def inceptionresnetv2(): base_model = InceptionResNetV2(include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=x) model.compile(optimizer='adam', loss='binary_crossentropy') return model
def build_model(): base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dropout(0.7)(x) predictions = Dense(len(class_names), activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def inceptionresnetv2(model_config, input_shape, metrics, n_classes, mixed_precision=False, output_bias=None): ''' Defines a model based on a pretrained InceptionResNetV2 for multiclass US classification. :param model_config: A dictionary of parameters associated with the model architecture :param input_shape: The shape of the model input :param metrics: Metrics to track model's performance :param n_classes: # of classes in data :param mixed_precision: Whether to use mixed precision (use if you have GPU with compute capacity >= 7.0) :param output_bias: bias initializer of output layer :return: a Keras Model object with the architecture defined in this method ''' # Set hyperparameters nodes_dense0 = model_config['NODES_DENSE0'] nodes_dense1 = model_config['NODES_DENSE1'] lr = model_config['LR'] dropout = model_config['DROPOUT'] l2_lambda = model_config['L2_LAMBDA'] optimizer = Adam(learning_rate=lr) print("MODEL CONFIG: ", model_config) if mixed_precision: tf.train.experimental.enable_mixed_precision_graph_rewrite(optimizer) if output_bias is not None: output_bias = Constant(output_bias) # Set initial output bias # Start with pretrained ResNet50V2 X_input = Input(input_shape, name='input') base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=input_shape, input_tensor=X_input) X = base_model.output # Add custom top layers X = GlobalAveragePooling2D()(X) X = Dropout(dropout)(X) X = Dense(nodes_dense0, kernel_initializer='he_uniform', activation='relu', activity_regularizer=l2(l2_lambda))(X) X = BatchNormalization()(X) X = Dense(n_classes, bias_initializer=output_bias)(X) Y = Activation('softmax', dtype='float32', name='output')(X) # Set model loss function, optimizer, metrics. model = Model(inputs=X_input, outputs=Y) model.summary() model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=metrics) return model
def get_model(args): logging.info('get model') # Get the InceptionV3 model so we can do transfer learning if args.base_modelname == 'inceptionV3': base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) elif args.base_modelname == 'inceptionResNetV2': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(args.IMG_WIDTH, args.IMG_WIDTH, 3)) out = base_model.output # out = Flatten()(out) out = GlobalAveragePooling2D()(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=predictions) elif args.base_modelname == 'MobileNetV2': base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(args.IMG_WIDTH, args.IMG_WIDTH, 3)) out = base_model.output out = GlobalAveragePooling2D()(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=predictions) else: x = Input(shape=(args.IMG_WIDTH, args.IMG_WIDTH, 3)) out = GlobalAveragePooling2D()(x) out = Dense(128, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=x, outputs=predictions) # model.compile(optimizer = RAdamOptimizer(learning_rate=1e-3), model.compile(optimizer=Adam(learning_rate=.0001), loss='categorical_crossentropy', metrics=['accuracy']) # model.summary() return model
def define_classifier_architecture(architecture_name, image_size, weights, classifier_kwargs=None): if architecture_name == 'MobileNet': model = MobileNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG16': model = VGG16(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG19': model = VGG19(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetMobile': model = NASNetMobile(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetLarge': model = NASNetLarge(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionV3': model = InceptionV3(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionResNetV2': model = InceptionResNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'Resnet50': model = ResNet50(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'EfficientNetB0': model = EfficientNetB0(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) else: raise ValueError( f"Classifier '{architecture_name}' is wrong or not implemented.") return model
def __init__(self, conf, extra_info): super(InceptionModel, self).__init__(conf, extra_info) width = conf['image']['width'] if 'random_crop' not in conf[ 'image'] else conf['image']['random_crop']['width'] height = conf['image']['height'] if 'random_crop' not in conf[ 'image'] else conf['image']['random_crop']['height'] channel = conf['image']['channel'] self.model = InceptionResNetV2(include_top=False, pooling='avg', weights='imagenet', input_shape=(width, height, channel))
def get_model(model_name): if model_name == 'VGG16': from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions model = VGG16(weights='imagenet') if model_name == 'VGG19': from tensorflow.keras.applications.vgg19 import VGG19 from tensorflow.keras.applications.vgg19 import preprocess_input, decode_predictions model = VGG19(weights='imagenet') elif model_name == 'ResNet50': from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions from tensorflow.keras.applications.resnet50 import ResNet50 model = ResNet50(weights='imagenet') elif model_name == 'InceptionV3': from tensorflow.keras.applications.inception_v3 import preprocess_input, decode_predictions from tensorflow.keras.applications.inception_v3 import InceptionV3 model = InceptionV3(weights='imagenet') elif model_name == 'InceptionResNetV2': from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 model = InceptionResNetV2(weights='imagenet') elif model_name == 'Xception': from tensorflow.keras.applications.xception import preprocess_input, decode_predictions from tensorflow.keras.applications.xception import Xception model = Xception(weights='imagenet') elif model_name == 'MobileNet': from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions from tensorflow.keras.applications.mobilenet import MobileNet model = MobileNet(weights='imagenet') elif model_name == 'MobileNetV2': from tensorflow.keras.applications.mobilenetv2 import preprocess_input, decode_predictions from tensorflow.keras.applications.mobilenetv2 import MobileNetV2 model = MobileNetV2(weights='imagenet') elif model_name == 'DenseNet': from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions from tensorflow.keras.applications.densenet import DenseNet121 model = DenseNet121(weights='imagenet') elif model_name == 'NASNet': from tensorflow.keras.applications.nasnet import preprocess_input, decode_predictions from tensorflow.keras.applications.nasnet import NASNetMobile model = NASNetMobile(weights='imagenet') elif model_name == 'EfficientNet': from efficientnet.tfkeras import EfficientNetB0 from keras.applications.imagenet_utils import decode_predictions from efficientnet.tfkeras import preprocess_input model = EfficientNetB7(weights='imagenet') else: print("[INFO] No model selected") return model, preprocess_input, decode_predictions
def save(self, *args, **kwargs): try: img = load_img(self.picture, target_size=(299, 299)) img_array = img_to_array(img) to_pred = np.expand_dims(img_array, axis=0) prep = preprocess_input(to_pred) model = InceptionResNetV2(weights="imagenet") prediction = model.predict(prep) decoded = decode_predictions(prediction)[0][0][1] self.classified = str(decoded) except Exception as e: print("error", e) super().save(*args, **kwargs)
def get_model(architecture, iteracion, models_info, pipeline): print("="*len(architecture)) print(architecture) print("="*len(architecture)) if iteracion > 0: base_model = models_info[architecture]['model_memory'] if architecture == 'InceptionV3': from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input if iteracion == 0: base_model = InceptionV3(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'InceptionV4': from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input if iteracion == 0: base_model = InceptionResNetV2(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'ResNet50': from tensorflow.keras.applications.resnet import ResNet50, preprocess_input if iteracion == 0: base_model = ResNet50(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'ResNet101': from tensorflow.keras.applications.resnet import ResNet101, preprocess_input if iteracion == 0: base_model = ResNet101(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'ResNet152': from tensorflow.keras.applications.resnet import ResNet152, preprocess_input if iteracion == 0: base_model = ResNet152(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'DenseNet121': from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input if iteracion == 0: base_model = DenseNet121(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'DenseNet169': from tensorflow.keras.applications.densenet import DenseNet169, preprocess_input if iteracion == 0: base_model = DenseNet169(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'DenseNet201': from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input if iteracion == 0: base_model = DenseNet201(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'NASNetLarge': from tensorflow.keras.applications.nasnet import NASNetLarge, preprocess_input if iteracion == 0: base_model = NASNetLarge(weights=pipeline['weights'],include_top=False,input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) if architecture == 'Xception': from tensorflow.keras.applications.xception import Xception, preprocess_input if iteracion == 0: base_model = Xception(weights=pipeline['weights'], include_top=False, input_shape=(pipeline['img_height'], pipeline['img_width'], 3)) return base_model, preprocess_input
def save(self, *args, **kwargs): try: img = load_img(self.picture.path, target_size=(299, 299)) img_arr = img_to_array(img) to_pred = np.expand_dims(img_arr, axis=0) # (1,299,299,3) prep = preprocess_input(to_pred) model = InceptionResNetV2(weights='imagenet') prediction = model.predict(prep) decoded = decode_predictions(prediction)[0][0][1] self.classified = str(decoded) print('Success') except Exception as e: print(f"Classification Failed {e}") super().save(*args, **kwargs)
def init_model(model_name): if (model_name == "VGG19"): return VGG19(include_top=True, weights='imagenet') if (model_name == "VGG16"): return tf.keras.applications.VGG16(include_top=True, weights='imagenet') if (model_name == "ResNet50"): return ResNet50(include_top=True, weights="imagenet") if (model_name == "DenseNet201"): return DenseNet201(include_top=True, weights="imagenet") if (model_name == "DenseNet121"): return DenseNet121(include_top=True, weights="imagenet") if (model_name == "InceptionResNetV2"): return InceptionResNetV2(include_top=True, weights="imagenet")
def __init__(self, model_config): super(InceptionResnetV2, self).__init__(model_config) self.name = 'Inception_ResnetV2' self.model = InceptionResNetV2( include_top=False, weights='imagenet' if self.use_imagenet_weights else None, input_shape=self.input_shape + (3, ), input_tensor=None, pooling='max', ) self.preprocess_func = preprocess_input self.setup()
def BuildNN(self, shape=(224, 224, 3), weight=None, loss=None): input_tensor = Input(shape=shape) base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input_tensor) for layer in base_model.layers: layer.trainable = True x = base_model.output x = GlobalAveragePooling2D()(x) x = Dropout(0.5)(x) # random projection idea x = Dense(256)(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Dropout(0.5)(x) # regular dense layer x = Dense(128)(x) x = LeakyReLU()(x) x = Dropout(0.5)(x) x = Dense(4, activation='sigmoid')(x) model = Model(inputs=input_tensor, outputs=x) if weight is not None: # load weights into new model model.load_weights(weight) print("Loaded model from disk") if loss is None: model.compile(loss='mse', optimizer='adam', metrics=['accuracy', iou_metric]) else: if loss == 'rmse': model.compile(loss=root_mean_squared_error, optimizer='adam', metrics=['accuracy', iou_metric]) elif loss == 'iou': model.compile(loss=iou_loss_v2, optimizer='adam', metrics=['accuracy', iou_metric]) return model
def Inception_ResNetV2_pretrain_model(): modelPre = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=config.input_shape, classes=config.category_num) model = tf.keras.Sequential() model.add(modelPre) model.add(tf.keras.layers.GlobalAveragePooling2D()) model.add( tf.keras.layers.Dense(config.category_num, name='fully_connected', activation='softmax', use_bias=False)) return model
def __get_model(): global __model if not __model: base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) base_model.trainable = False # Add Layer Embedding __model = Sequential([ base_model, GlobalMaxPooling2D() ]) return __model
def save(self, *args, **kwargs): try: # print(self.picture) img = load_img(self.picture, target_size=(299, 299)) img_array = img_to_array(img) to_pred = np.expand_dims(img_array, axis=0) prep = preprocess_input(to_pred) model = InceptionResNetV2(weights='imagenet') prediction = model.predict(prep) decoded = decode_predictions(prediction)[0][0][1] self.classified = decoded print("success") except: print('classification failed') super().save(*args, **kwargs)
def get_model_inception_resnet(num_class): base_model = InceptionResNetV2(weights='imagenet', include_top=False) x = base_model.output # custom top predictions = get_custom_top(x, num_class) model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return model
def save(self, *args, **kwargs): try: img = load_img(self.picture, target_size=(299, 299)) img_array = img_to_array(img) to_predict = np.expand_dims(img_array, axis=0) preprossesing = preprocess_input(to_predict) model = InceptionResNetV2(weights='imagenet') prediction = model.predict(preprossesing) decode = decode_predictions(prediction)[0][0][1] self.classified = str(decode) print('success') except Exception as e: print("classification failed", e) super().save(*args, **kwargs)
def __init__(self, input_shape, seq_length, rnn_size, charset, num_views): super().__init__() self._mparams = default_mparams() self.num_char_classes = len(charset) self.seq_length = seq_length self.charset = charset self.num_views = num_views self.feature_enc = InceptionResNetV2( weights='imagenet', input_shape=input_shape, include_top=False) self.feature_enc.outputs = [ self.feature_enc.get_layer('mixed_6a').output] self.feature_enc.trainable = False self.rnn = ChaRNN(rnn_size, self.seq_length, self.num_char_classes) self.character_mapper = CharsetMapper(self.charset, self.seq_length)
def load_model(self): FACTOR = 0.70 HEIGHT = 137 WIDTH = 236 HEIGHT_NEW = int(HEIGHT * FACTOR) WIDTH_NEW = int(WIDTH * FACTOR) base_model = InceptionResNetV2(include_top=False, input_tensor=None, input_shape=(HEIGHT_NEW, WIDTH_NEW, 3), pooling=None, classes=1000) # base_model.trainable=False x = base_model.output x = layers.GlobalAveragePooling2D()(x) grapheme_root = layers.Dense(168, activation='softmax', name='root')(x) vowel_diacritic = layers.Dense(11, activation='softmax', name='vowel')(x) consonant_diacritic = layers.Dense(7, activation='softmax', name='consonant')(x) model = Model( inputs=base_model.input, outputs=[grapheme_root, vowel_diacritic, consonant_diacritic]) # for layer in base_model.layers: # layer.trainable = True model.compile(optimizer='adam', loss={ 'root': 'categorical_crossentropy', 'vowel': 'categorical_crossentropy', 'consonant': 'categorical_crossentropy' }, loss_weights={ 'root': 0.5, 'vowel': 0.25, 'consonant': 0.25 }, metrics={ 'root': 'accuracy', 'vowel': 'accuracy', 'consonant': 'accuracy' }) print(model.summary()) return model
def __init__(self, model_config): super(InceptionResnetV2, self).__init__(model_config) self.name = 'Inception_ResnetV2' self.model = InceptionResNetV2( include_top=False, weights='imagenet' if self.use_imagenet_weights else None, input_shape=self.input_shape + [ 3, ], input_tensor=None, pooling='max', ) self._config[ 'preprocess_func'] = 'tensorflow.keras.applications.inception_resnet_v2.preprocess_input' self.setup()