def create_MobileNet(self, output_len, inp_shape): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=inp_shape, alpha=1.0, include_top=True, weights=None, pooling=None) # model_json = mobilenet_model.to_json() # # with open("mobileNet_v2_main.json", "w") as json_file: # json_file.write(model_json) # # return mobilenet_model mobilenet_model.layers.pop() x = mobilenet_model.get_layer( 'global_average_pooling2d_1').output # 1280 out_landmarks = Dense(output_len, name='O_L')(x) out_poses = Dense(LearningConfig.pose_len, name='O_P')(x) inp = mobilenet_model.input revised_model = Model(inp, [out_landmarks]) # revised_model = Model(inp, [out_landmarks, out_poses]) revised_model.summary() return revised_model
def mobileNet_v2_main(self, tensor): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights=None, input_tensor=tensor, pooling=None) # , classes=cnf.landmark_len) mobilenet_model.layers.pop() x = mobilenet_model.get_layer( 'global_average_pooling2d_1').output # 1280 x = Dense(LearningConfig.landmark_len, name='dense_layer_out_2', activation='relu', kernel_initializer='he_uniform')(x) Logits = Dense(LearningConfig.landmark_len, name='out')(x) inp = mobilenet_model.input revised_model = Model(inp, Logits) revised_model.summary() # plot_model(revised_model, to_file='mobileNet_v2_main.png', show_shapes=True, show_layer_names=True) model_json = revised_model.to_json() with open("mobileNet_v2_main.json", "w") as json_file: json_file.write(model_json) return revised_model
def mobileNet_v2_main_discriminator(self, tensor, input_shape): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=input_shape, alpha=1.0, include_top=True, weights=None, input_tensor=tensor, pooling=None) # , classes=cnf.landmark_len) mobilenet_model.layers.pop() x = mobilenet_model.get_layer( 'global_average_pooling2d_2').output # 1280 softmax = Dense(1, activation='sigmoid', name='out')(x) inp = mobilenet_model.input revised_model = Model(inp, softmax) revised_model.summary() # plot_model(revised_model, to_file='mobileNet_v2_main.png', show_shapes=True, show_layer_names=True) model_json = revised_model.to_json() with open("mobileNet_v2_main.json", "w") as json_file: json_file.write(model_json) return revised_model
def load_model(self, model_instance=False): """Load a pretrained model""" if not model_instance: from keras.applications import mobilenet_v2 self.model = mobilenet_v2.MobileNetV2(input_shape=(self.h, self.w, 3))
def keras_clf(): # This is a small classifier (~14 MB, ~3.5 million weights). # On first run weights are downloaded automatically and cached. # See https://keras.io/applications/ clf = mobilenet_v2.MobileNetV2(alpha=1.0, include_top=True, weights='imagenet', classes=1000) print('Summary of classifier:') clf.summary() return clf
def get_model(model, **kwargs): if model == 'vgg16': return vgg16.VGG16(**kwargs), vgg16.preprocess_input if model == 'resnet50v2': return resnetv2.ResNet50V2(**kwargs), resnetv2.preprocess_input if model == 'mobilenetv2': return mobilenetv2.MobileNetV2(**kwargs), mobilenetv2.preprocess_input raise ValueError
def get_architecture(self, params): from keras import models from keras.applications import mobilenet_v2 from keras.layers import Dense, Dropout, MaxPooling2D, BatchNormalization, Flatten, Conv2D, Conv3D, MaxPooling3D, AveragePooling3D, Softmax, AveragePooling2D # input_shape = params['input_shape'] #TODO: check_dims initial_model = mobilenet_v2.MobileNetV2( ) # TODO: tal vez incluir image_data_format='channels_last' for i in range(len(initial_model.layers) - 1 - params['last_layers']): initial_model.layers[i].trainable = False model = initial_model.layers[len(initial_model.layers) - 1 - params[ 'last_layers']].output # assuming you want the 3rd layer from the last if not params['use_sklearn']: dimensions = params['custom_dimension_list'] layers = params['custom_layer_list'] for i in range(len(layers)): if (layers[i].lower() == 'conv3'): model = Conv3D(params['batch'], kernel_size=dimensions[i], strides=(1, 1, 1), activation=params['activation'])(model) elif (layers[i].lower() == 'maxpool3'): model = MaxPooling3D(pool_size=dimensions[i])(model) elif (layers[i].lower() == 'avgpool3'): model = AveragePooling3D(pool_size=dimensions[i])(model) elif (layers[i].lower() == 'conv2'): model = Conv2D(params['batch'], kernel_size=dimensions[i], strides=(1, 1), activation=params['activation'])(model) elif (layers[i].lower() == 'maxpool2'): model = MaxPooling2D(pool_size=dimensions[i])(model) elif (layers[i].lower() == 'avgpool2'): model = AveragePooling2D(pool_size=dimensions[i])(model) elif (layers[i].lower() == 'drop'): model = Dropout(dimensions[i])(model) elif (layers[i].lower() == 'flatten'): model = Flatten()(model) elif (layers[i].lower() == 'norm'): model = BatchNormalization(axis=-1)(model) elif (layers[i].lower() == 'dense'): model = Dense(dimensions[i], activation=params['activation'])(model) elif (layers[i].lower() == 'softmax'): model = Softmax()(model) else: model = Flatten()(model) model = models.Model(initial_model.input, model) return model
def mnv2_hm(self, tensor): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights=None, input_tensor=tensor, pooling=None) mobilenet_model.layers.pop() inp = mobilenet_model.input '''heatmap can not be generated from activation layers, so we use out_relu''' x = mobilenet_model.get_layer('out_relu').output # 7, 7, 1280 x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name='deconv1', kernel_initializer='he_uniform')(x) # 14, 14, 256 x = BatchNormalization(name='out_bn1')(x) x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name='deconv2', kernel_initializer='he_uniform')(x) # 28, 28, 256 x = BatchNormalization(name='out_bn2')(x) x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name='deconv3', kernel_initializer='he_uniform')(x) # 56, 56, 256 x = BatchNormalization(name='out_bn3')(x) out_heatmap = Conv2D(LearningConfig.landmark_len // 2, kernel_size=1, padding='same', name='out_heatmap')(x) revised_model = Model(inp, [ out_heatmap, ]) revised_model.summary() # plot_model(revised_model, to_file='mnv2_hm.png', show_shapes=True, show_layer_names=True) model_json = revised_model.to_json() with open("mnv2_hm.json", "w") as json_file: json_file.write(model_json) return revised_model
def exercise_five(): # na koniec skorzystamy z gotowej, wytrenowanej juz sieci glebokiej # TODO: uruchom przyklad pobierajacy gotowa siec, wytrenowana na zbiorze ImageNet # pobranie gotowego modelu zlozonej sieci konwolucyjnej z odpowiednimi wagami # include_top=True oznacza ze pobieramy wszystkie warstwy - niektore zastosowania korzystaja tylko z dolnych model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) # podejrzenie tego z ilu i jakich warstw sie sklada layers = dict([(layer.name, layer.output) for layer in model.layers]) print("Network containts following layers:") for i, (name, layer) in enumerate(layers.items()): print("Layer {0} : {1}".format(i, (name, layer))) # oraz ile parametrow musialo zostac wytrenowanych print("Together: {0} parameters\n".format(model.count_params())) # TODO: odpowiedz, o ile wieksza jest ta siec od wytrenowanej przez nas? # powyzsza siec jest i tak wyjatkowo mala - sluzy do zastosowan mobilnych, wiec jest silnie miniaturyzowana # otworzmy przykladowe zdjecie i dostosujemy jego rozmiar i zakres wartosci do wejscia sieci image_path = 'nosacz.jpg' image = k_image.load_img(image_path, target_size=(224, 224)) # TODO: zastap None powyzej zmieniajac rozmiar na taki, jaki przyjmuje wejscie sieci (skorzystaj z wypisanego info) x = k_image.img_to_array( image) # kolejne linie dodatkowo dostosowuja obraz pod dana siec x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) # sprawdzmy jaki wynik przewidzi siec predictions = model.predict(x) # i przetlumaczmy uzywajac etykiet zrozumialych dla czlowieka (5 najbardziej prawdopodobnych klas zdaniem sieci) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0]) # TODO: czy skonczylo sie sukcesem? # TODO: pobaw sie z innymi zdjeciami z Internetu - jak radzi sobie siec? kiedy sie myli? w jaki sposob? # TODO: (c.d.) pamietaj, ze siec rozumie tylko wymienione w ponizszym JSONIE klasy: # https://github.com/raghakot/keras-vis/blob/master/resources/imagenet_class_index.json # finalnie podgladamy aktywacje jakie wysylaja neurony sieci w trakcie dzialania # w wypisanych wczesniej informacjach mozna latwo spradzic ile kanalow ma warstwa o danym numerze (i ktora to) # layer_to_preview = 4 # numer warstwy, ktorej aktywacje podgladamy layer_to_preview = 65 # numer warstwy, ktorej aktywacje podgladamy # channel_to_preview = 16 # numer kanalu w tejze warstwie channel_to_preview = 0 # numer kanalu w tejze warstwie get_activations = k.function([model.layers[0].input], [model.layers[layer_to_preview].output]) activations = get_activations([x]) plt.imshow(activations[0][0, :, :, channel_to_preview], cmap="viridis") plt.show() # TODO: podejrzyj aktywacje w kolejnych warstwach; czym roznia sie te w poczatkowych od tych w koncowych? # o. ..0000.00 0.0 o.0 # colory znikajo, i rozdzielozcosc znika return model
def home_two(): model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) image_path = 'RTR6S1V.jpg' #224=7*2^5 image = k_image.load_img(image_path, target_size=(224, 224)) print("running") # TODO: zastap None powyzej zmieniajac rozmiar na taki, jaki przyjmuje wejscie sieci (skorzystaj z wypisanego info) x = k_image.img_to_array( image) # kolejne linie dodatkowo dostosowuja obraz pod dana siec x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) out_array = np.zeros( (224, 224, 2) ) # first is sum of % outcomes, second is number of tries later they will be divided size = 7 * 2 # 227/size must be int for i1 in range(224 - size): for k1 in range(224 - size): x_clone = np.copy(x) add_dark_square(x_clone, i1, k1, size) predictions = model.predict([x_clone]) add_to_all( out_array, i1, k1, size, k_mobilenet_v2.decode_predictions( predictions, top=5)[0][0][2]) # TODO cahange 0.5 to actual outcome heat_map = np.zeros((224, 224)) min_val = 1 max_val = 0 for i in range(224): for k in range(224): heat_map[i][k] = out_array[i][k][0] / out_array[i][k][1] if heat_map[i][k] < min_val: min_val = heat_map[i][k] if heat_map[i][k] > max_val: max_val = heat_map[i][k] plt.imshow(heat_map, cmap="hot", vmin=min_val, vmax=max_val) plt.show() print(heat_map) print(max_val) print(min_val) print("\n\n\n\n") print(type(x)) print(x) print(x.shape) predictions = model.predict(x) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0]) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0][0][1])
def _init_model(self): """ Loads and sets up VGG19 model """ # load VGG19 model with pre-trainde ImageNet weights self.model = mobilenet_v2.MobileNetV2(input_tensor=self.input_tensor, weights='imagenet', include_top=False) # get outputs by layer name self.layers_outputs = { layer.name: layer.output for layer in self.model.layers }
def _create_MobileNet_with_embedding_single(self, num_of_classes, input_shape): mobilenet_model_face = mobilenet_v2.MobileNetV2( input_shape=input_shape, alpha=1.0, include_top=True, weights=None, input_tensor=None, pooling=None) mobilenet_model_face.layers.pop() global_average_pooling2d = mobilenet_model_face.get_layer( 'global_average_pooling2d').output # 1280 x_l_face = tf.keras.layers.Dense( LearningConfig.embedding_size, activation=None)( global_average_pooling2d) # No activation on final dense layer embedding_layer_face = tf.keras.layers.Lambda( lambda x: tf.math.l2_normalize(x, axis=1))( x_l_face) # L2 normalize embeddings '''FC layer for out''' x_l = Dense(LearningConfig.embedding_size * 2)(global_average_pooling2d) x_l = BatchNormalization()(x_l) x_l = ReLU()(x_l) x_l = Dense(LearningConfig.embedding_size)(x_l) x_l = BatchNormalization()(x_l) x_l = ReLU()(x_l) x_l = Dense(LearningConfig.embedding_size // 2)(x_l) x_l = BatchNormalization()(x_l) x_l = ReLU()(x_l) '''Dropout''' x_l = Dropout(rate=0.2)(x_l) '''out''' out_categorical = Dense( num_of_classes, kernel_regularizer=tf.keras.regularizers.l2(0.0001), activation='softmax', # activation='linear', name='out')(x_l) inp = [mobilenet_model_face.input] revised_model = Model(inp, [out_categorical, embedding_layer_face]) revised_model.summary() '''save json''' model_json = revised_model.to_json() with open("./model_archs/mn_v2_cat_emb_sinle.json", "w") as json_file: json_file.write(model_json) return revised_model
def create_ASMNet(self, output_len, inp_tensor=None, inp_shape=None): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=inp_shape, alpha=1.0, include_top=True, weights=None, input_tensor=inp_tensor, pooling=None) mobilenet_model.layers.pop() inp = mobilenet_model.input '''heatmap can not be generated from activation layers, so we use out_relu''' block_1_project_BN = mobilenet_model.get_layer('block_1_project_BN').output # 56*56*24 block_1_project_BN_mpool = GlobalAveragePooling2D()(block_1_project_BN) block_3_project_BN = mobilenet_model.get_layer('block_3_project_BN').output # 28*28*32 block_3_project_BN_mpool = GlobalAveragePooling2D()(block_3_project_BN) block_6_project_BN = mobilenet_model.get_layer('block_6_project_BN').output # 14*14*64 block_6_project_BN_mpool = GlobalAveragePooling2D()(block_6_project_BN) block_10_project_BN = mobilenet_model.get_layer('block_10_project_BN').output # 14*14*96 block_10_project_BN_mpool = GlobalAveragePooling2D()(block_10_project_BN) block_13_project_BN = mobilenet_model.get_layer('block_13_project_BN').output # 7*7*160 block_13_project_BN_mpool = GlobalAveragePooling2D()(block_13_project_BN) block_15_add = mobilenet_model.get_layer('block_15_add').output # 7*7*160 block_15_add_mpool = GlobalAveragePooling2D()(block_15_add) x = keras.layers.Concatenate()([block_1_project_BN_mpool, block_3_project_BN_mpool, block_6_project_BN_mpool, block_10_project_BN_mpool, block_13_project_BN_mpool, block_15_add_mpool]) x = keras.layers.Dropout(rate=0.3)(x) '''''' out_landmarks = Dense(output_len, kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01), name='O_L')(x) out_poses = Dense(LearningConfig.pose_len, kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01), name='O_P')(x) revised_model = Model(inp, [out_landmarks, out_poses]) revised_model.summary() model_json = revised_model.to_json() with open("ASMNet.json", "w") as json_file: json_file.write(model_json) return revised_model
def mobilenet_v2_retinanet(num_classes, backbone='mobilenet_v2_1.0_224', inputs=None, modifier=None, **kwargs): """ Constructs a retinanet model using a mobilenet backbone. Args num_classes: Number of classes to predict. backbone: Which backbone to use (one of ('mobilenet_v2_96', mobilenet_v2_128', 'mobilenet_v2_160', 'mobilenet_v2_192', 'mobilenet_v2_224')). inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)). modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example). Returns RetinaNet model with a MobileNet backbone. """ alpha = float(backbone.split('_')[2]) # choose default input if inputs is None: inputs = keras.layers.Input((None, None, 3)) backbone = mobilenet_v2.MobileNetV2(input_tensor=inputs, alpha=alpha, include_top=False, pooling=None, weights=None) bn_layers = [ l for l in backbone.layers if type(l) == keras.layers.normalization.BatchNormalization ] for bn in bn_layers: bn.trainable = False # create the full model layer_names = ['block_4_project', 'block_10_project', 'block_16_project'] layer_outputs = [backbone.get_layer(name).output for name in layer_names] backbone = keras.models.Model(inputs=inputs, outputs=layer_outputs, name=backbone.name) # invoke modifier if given if modifier: backbone = modifier(backbone) return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=backbone.outputs, **kwargs)
def __init__(self): self.graph = tf.get_default_graph() self.model = mobilenet_v2.MobileNetV2( weights='imagenet' ) # The first time you run this script, Google's pretrained network weights are downloaded. This may take a few minutes so don't worry if it seems to "stall out" print(self.model.summary() ) # Print a summary of the model's architecture plot_model(self.model, to_file='mobilenet.png' ) # This creates a .png showing the model's architecture #pdb.set_trace() # You may want to use PDB to step through code here self.bridge = CvBridge() self.image_sub = rospy.Subscriber('image_topic_2', Image, self.image_callback) self.pub = rospy.Publisher("label", String, queue_size=10)
def create_mobileNet(self, output_len, inp_shape): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=inp_shape, alpha=1.0, include_top=True, weights=None, pooling=None) mobilenet_model.layers.pop() x = mobilenet_model.get_layer('global_average_pooling2d_1').output # 1280 out_landmarks = Dense(output_len, name='O_L')(x) out_poses = Dense(LearningConfig.pose_len, name='O_P')(x) inp = mobilenet_model.input revised_model = Model(inp, [out_landmarks, out_poses]) revised_model.summary() return revised_model
def create_branch_mn(self, prefix, inp_shape): mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=inp_shape, alpha=1.0, include_top=True, weights=None, input_tensor=None, pooling=None) mobilenet_model.layers.pop() inp = mobilenet_model.input '''heatmap can not be generated from activation layers, so we use out_relu''' x = mobilenet_model.get_layer('out_relu').output # 7, 7, 1280 x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name=prefix + '_deconv1', kernel_initializer='he_uniform')(x) # 14, 14, 256 x = BatchNormalization(name=prefix + 'out_bn1')(x) x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name=prefix + '_deconv2', kernel_initializer='he_uniform')(x) # 28, 28, 256 x = BatchNormalization(name=prefix + 'out_bn2')(x) x = Deconvolution2D(filters=256, kernel_size=(2, 2), strides=(2, 2), padding='same', activation='relu', name=prefix + '_deconv3', kernel_initializer='he_uniform')(x) # 56, 56, 256 x = BatchNormalization(name=prefix + 'out_bn3')(x) out_heatmap = Conv2D(LearningConfig.point_len, kernel_size=1, padding='same', name=prefix + '_out_hm')(x) for layer in mobilenet_model.layers: layer.name = layer.name + '_' + prefix return inp, out_heatmap
def build_mobilenetv2(weights='imagenet', fine_tune=None): """ Builds and compiles the MobileNetV2 model using pre-trained weights and custom classification head. :param weights: Pre-trained weights to be used :param fine_tune: First layer of each block to be fine-tuned :return: The compiled model """ if fine_tune is None: fine_tune = ['block_4_expand'] input_shape = (224, 224, 3) # Configure base model from pretrained model_mobilenet = mobilenet_v2.MobileNetV2(include_top=False, weights=weights, input_shape=input_shape) # Set blocks 4 and down to be fine tuneable model_mobilenet.trainable = True set_trainable = False for layer in model_mobilenet.layers: if layer.name in fine_tune: set_trainable = True if set_trainable: layer.trainable = True else: layer.trainable = False inputs = Input((224, 224, 3)) x = model_mobilenet(inputs) out1 = GlobalMaxPooling2D()(x) out2 = GlobalAveragePooling2D()(x) out3 = Flatten()(x) out = Concatenate(axis=-1)([out1, out2, out3]) out = Dropout(0.5)(out) out = Dense(1, activation="sigmoid", name="3_")(out) model = Model(inputs, out) # compile model model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(0.0001), metrics=['accuracy']) # print model summary print(model.summary()) return model
def load_model(self, model_instance=False): """Load a pretrained model""" if not model_instance: try: device_name = os.environ['COLAB_TPU_ADDR'] TPU_ADDRESS = 'grpc://' + device_name print('Found TPU at: {}'.format(TPU_ADDRESS)) except KeyError: print('TPU not found') from keras.applications import mobilenet_v2 self.model = mobilenet_v2.MobileNetV2(input_shape=(self.h, self.w, 3)) #, depth_multiplier=self.depth_multiplier) self.model = tf.tf.contrib.tpu.keras_to_tpu_model( self.model, strategy=tf.contrib.tpu.TPUDistributionStrategy( tf.contrib.cluster_resolver.TPUClusterResolver( TPU_ADDRESS)))
def create_MobileNet_nopose(self, inp_shape, output_len): # initializer = tf.keras.initializers.RandomUniform(minval=0.0, maxval=0.01, seed=None) # initializer = tf.keras.initializers.HeUniform() initializer = tf.keras.initializers.glorot_uniform() mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=inp_shape, alpha=1.0, include_top=True, weights=None, input_tensor=None, pooling=None) # model_json = mobilenet_model.to_json() # # with open("mobileNet_v2_main.json", "w") as json_file: # json_file.write(model_json) # # return mobilenet_model mobilenet_model.layers.pop() x = mobilenet_model.get_layer( 'global_average_pooling2d').output # 1280 x = Dropout(0.1)(x) out_landmarks = Dense(output_len, activation=keras.activations.linear, kernel_initializer=initializer, use_bias=True, name='O_L')(x) inp = mobilenet_model.input revised_model = Model(inp, [out_landmarks]) revised_model.summary() # plot_model(revised_model, to_file='mobileNet_v2_main.png', show_shapes=True, show_layer_names=True) model_json = revised_model.to_json() with open("mobileNet_v2_main.json", "w") as json_file: json_file.write(model_json) return revised_model
def exercise_five(): # na koniec skorzystamy z gotowej, wytrenowanej juz sieci glebokiej # pobranie gotowego modelu zlozonej sieci konwolucyjnej z odpowiednimi wagami # include_top=True oznacza ze pobieramy wszystkie warstwy - niektore zastosowania korzystaja tylko z dolnych model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) # podejrzenie tego z ilu i jakich warstw sie sklada layers = dict([(layer.name, layer.output) for layer in model.layers]) print("Network containts following layers:") for i, (name, layer) in enumerate(layers.items()): print("Layer {0} : {1}".format(i, (name, layer))) # oraz ile parametrow musialo zostac wytrenowanych print("Together: {0} parameters\n".format(model.count_params())) # powyzsza siec jest i tak wyjatkowo mala - sluzy do zastosowan mobilnych, wiec jest silnie miniaturyzowana # otworzmy przykladowe zdjecie i dostosujemy jego rozmiar i zakres wartosci do wejscia sieci image_path = 'meat-loaf.jpeg' image = k_image.load_img(image_path, target_size=(224, 224)) x = k_image.img_to_array( image) # kolejne linie dodatkowo dostosowuja obraz pod dana siec x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) # sprawdzmy jaki wynik przewidzi siec predictions = model.predict(x) # i przetlumaczmy uzywajac etykiet zrozumialych dla czlowieka (5 najbardziej prawdopodobnych klas zdaniem sieci) print('Predicted class:', k_mobilenet_v2.decode_predictions(predictions, top=5)[0]) # https://github.com/raghakot/keras-vis/blob/master/resources/imagenet_class_index.json # finalnie podgladamy aktywacje jakie wysylaja neurony sieci w trakcie dzialania # w wypisanych wczesniej informacjach mozna latwo spradzic ile kanalow ma warstwa o danym numerze (i ktora to) layer_to_preview = 10 # numer warstwy, ktorej aktywacje podgladamy channel_to_preview = 16 # numer kanalu w tejze warstwie get_activations = k.function([model.layers[0].input], [model.layers[layer_to_preview].output]) activations = get_activations([x]) plt.imshow(activations[0][0, :, :, channel_to_preview], cmap="viridis") plt.show()
def homework2(): PATCH_SIZE = 56 STEP = PATCH_SIZE // 8 IMG_SIZE = 224 model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) image_path = sys.argv[1] if len(sys.argv) > 1 else "nosacz.jpg" image = k_image.load_img(image_path, target_size=(IMG_SIZE, IMG_SIZE)) x = k_image.img_to_array(image) x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) predictions = model.predict(x) (_, name, bench_prob) = k_mobilenet_v2.decode_predictions(predictions)[0][0] print(name, bench_prob) counts = np.zeros(shape=(IMG_SIZE, IMG_SIZE)) averages = np.zeros(shape=(IMG_SIZE, IMG_SIZE)) for xoffset, yoffset in product(range(0, IMG_SIZE - PATCH_SIZE + 1, STEP), repeat=2): print(xoffset, yoffset) x = k_image.img_to_array(image) x[xoffset:xoffset + PATCH_SIZE, yoffset:yoffset + PATCH_SIZE, :] = 200 x = np.expand_dims(x, axis=0) x = k_mobilenet_v2.preprocess_input(x) predictions = model.predict(x) print(k_mobilenet_v2.decode_predictions(predictions, top=3)[0]) d = predictions_dict(predictions) value = d[name] counts[xoffset:xoffset + PATCH_SIZE, yoffset:yoffset + PATCH_SIZE] += 1 averages[xoffset:xoffset + PATCH_SIZE, yoffset:yoffset + PATCH_SIZE] += value / bench_prob averages = averages / counts plt.imshow(averages, cmap='jet') filename = image_path + '.heatmap.png' plt.savefig(filename, bbox_inches='tight')
def mobilenet_retinanet(num_classes, backbone='mobilenet224_1.0', inputs=None, modifier=None, **kwargs): """ Constructs a retinanet model using a mobilenet backbone. Args num_classes: Number of classes to predict. backbone: Which backbone to use (one of ('mobilenet128', 'mobilenet160', 'mobilenet192', 'mobilenet224')). inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)). modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example). Returns RetinaNet model with a MobileNet backbone. """ try: alpha = float(backbone.split('_')[1]) except IndexError: alpha = 1.0 # choose default input if inputs is None: inputs = keras.layers.Input((None, None, 3)) backbone = mobilenet_v2.MobileNetV2(input_tensor=inputs, alpha=alpha, include_top=False, pooling=None, weights=None) # create the full model layer_names = ['block_6_expand_relu', 'block_13_expand_relu', 'out_relu'] layer_outputs = [backbone.get_layer(name).output for name in layer_names] backbone = keras.models.Model(inputs=inputs, outputs=layer_outputs, name=backbone.name) # invoke modifier if given if modifier: backbone = modifier(backbone) return retinanet.retinanet(inputs=inputs, num_classes=num_classes, create_pyramid_features=__create_pyramid_features, backbone_layers=backbone.outputs, **kwargs)
#!/usr/bin/env python3 from tvm import relay from keras.applications import mobilenet_v2 from tvm_ec2_compiler_utils import tvm_compile keras_model = mobilenet_v2.MobileNetV2() #keras_model.summary() shape_dict = {'input_1': (1, 3, 224, 224)} dlr_model_name = "dlr_keras_mobilenet_v2" print("Model:", keras_model.name, ", shape_dict:", shape_dict) for arch in ["c4", "c5", "p3"]: sym, params = relay.frontend.from_keras(keras_model, shape_dict) func = sym["main"] tvm_compile(func, params, arch, dlr_model_name)
from keras.preprocessing.image import ImageDataGenerator, load_img from matplotlib import pyplot as plt import numpy as np from keras.applications import mobilenet_v2 from keras import models from keras import layers from keras import optimizers mobnet_conv = mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0, include_top=False, weights='imagenet', input_tensor=None, pooling=None) train_dir = './clean-dataset/train' validation_dir = './clean-dataset/validation' # nTrain = 3840 nTrain = 20 nVal = 960 # nVal = 200 # nImages = 4800 datagen = ImageDataGenerator(rescale=1. / 255) batch_size = 20 train_features = np.zeros(shape=(nTrain, 7, 7, 1280)) train_labels = np.zeros(shape=(nTrain, 2)) train_generator = datagen.flow_from_directory(train_dir, target_size=(224, 224),
y_test = np.array(data_06.item().get('bbs')) x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = np.reshape(x_train, (-1, img_size, img_size, 3)) x_test = np.reshape(x_test, (-1, img_size, img_size, 3)) y_train = np.reshape(y_train, (-1, output_size)) y_test = np.reshape(y_test, (-1, output_size)) inputs = Input(shape=(img_size, img_size, 3)) # Pretrained model mobilenet_model = mobilenet_v2.MobileNetV2(input_shape=(img_size, img_size, 3), alpha=1.0, include_top=False, weights='imagenet', input_tensor=inputs, pooling='max') net = Dense(128, activation='relu')(mobilenet_model.layers[-1].output) net = Dense(64, activation='relu')(net) net = Dense(output_size, activation='linear')(net) model = Model(inputs=inputs, outputs=net) model.summary() print('Building finished.') # Start training print('Training models...') model.compile(optimizer=keras.optimizers.Adam(), loss='mse')
# Use this code as a sample to create a node capable of classifying the contents of an image # For use in X-491, Spring 2019 import keras, tensorflow, numpy, pdb # Import Keras, Tensorflow, and NumPY (API calls, low-level deep learning software, and math package) from keras.applications import mobilenet_v2 # A mobilenet is useful because our VMs are limited computationally -- this network is designed to run on a mobile device, like a cell phone from keras.utils.vis_utils import plot_model # We use this to print the model description from keras.preprocessing.image import load_img, img_to_array from keras.applications.mobilenet_v2 import preprocess_input, decode_predictions model = mobilenet_v2.MobileNetV2( weights='imagenet' ) # The first time you run this script, Google's pretrained network weights are downloaded. This may take a few minutes so don't worry if it seems to "stall out" print(model.summary()) # Print a summary of the model's architecture plot_model(model, to_file='mobilenet.png' ) # This creates a .png showing the model's architecture #pdb.set_trace() # You may want to use PDB to step through code here ## TODO: write preprocessing here - you can use OpenCV or keras.preprocessing # The model expects images of a particular size here - you can use opencv, or image=load_img(filehandle, target_size=(X,Y)) # The image size is called out in the first layer of the model (X,Y,3) for the (X,Y,RGB) dimensions # If you don't resize, the model will work poorly or not at all # Once your image is resized, convert it to an array image = img_to_array(image) # The model expects data in a particular format: (Samples, rows, columns, channels) # We only have one file, so use reshape to add the sample dimension # image = image.reshape((A, X, Y, 3))
test_labels.append(label) data = np.array(data, dtype="float") / 255.0 labels = np.array(labels, dtype="int") test_data = np.array(test_data, dtype="float") / 255.0 test_labels = np.array(test_labels, dtype="int") labelsY = to_categorical(labels, num_classes=3) testY = to_categorical(test_labels, num_classes=3) totalTrain = len(list(paths.list_images(config.TRAIN_PATH))) #model = AlexNet.build() base = mobilenet_v2.MobileNetV2(weights='imagenet', include_top=False) x = base.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) x = Dense(1024, activation='relu')(x) x = Dense(512, activation='relu')(x) preds = Dense(5, activation='softmax')(x) model = Model(inputs=base.input, outputs=preds) model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy']) trainAug = ImageDataGenerator(rescale=1 / 255.0,
import numpy as np import tensorflow as tf from keras.applications import mobilenet_v2 mobile_net = mobilenet_v2.MobileNetV2( alpha=0.35, weights= "/model/files/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.35_224") graph = tf.get_default_graph() def predict(input): images = mobilenet_v2.preprocess_input(input) with graph.as_default(): probas = mobile_net.predict(images) classes = probas.argmax(axis=1) return { "classes": classes.astype("int64"), "probabilities": probas.astype("double") }
def test_cam(): model = k_mobilenet_v2.MobileNetV2(weights='imagenet', include_top=True) image_path = 'img/doberman.jpg' generate_class_activation_map(model, image_path, 224, 5)