def Run(self, img_path, model_name): # config variables weights = 'imagenet' include_top = 0 train_path = 'jpg' classfier_file = 'output/flowers_17/' + model_name + '/classifier.cpickle' # create the pretrained models # check for pretrained weight usage or not # check for top layers to be included or not if model_name == "vgg16": from vgg16 import VGG16, preprocess_input base_model = VGG16(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "vgg19": from vgg19 import VGG19, preprocess_input base_model = VGG19(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": from resnet50 import ResNet50, preprocess_input base_model = ResNet50(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (224, 224) elif model_name == "inceptionv3": from inception_v3 import InceptionV3, preprocess_input base_model = InceptionV3(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('mixed9').output) image_size = (299, 299) elif model_name == "xception": from xception import Xception, preprocess_input base_model = Xception(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None img = image.load_img(img_path, target_size=image_size) img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) feature = model.predict(img_array) feature = feature.flatten() with open(classfier_file, 'rb') as f: model2 = pickle.load(f) pred = model2.predict(feature) prob = model2.predict_proba(np.atleast_2d(feature))[0] return pred, prob[0]
def load_model (args): if args.output_layer == '0': if args.model == 'inception': model = InceptionV3(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'xception': model = Xception(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'inceptionresnet': model = InceptionResNetV2(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'mobilenet': model = MobileNet(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'mobilenet2': model = MobileNetV2(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'nasnet': model = NASNetLarge(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='tf' elif args.model == 'resnet': model = ResNet50(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='caffe' elif args.model == 'vgg16': model = VGG16(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='caffe' elif args.model == 'vgg19': model = VGG19(include_top=False, weights='imagenet', pooling=args.pooling) preprocess_mode='caffe' else: print ("Model not found") return 0 else: if args.model == 'inception': base_model = InceptionV3(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'xception': base_model = Xception(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'inceptionresnet': base_model = InceptionResNetV2(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'mobilenet': base_model = MobileNet(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'mobilenet2': base_model = MobileNetV2(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'nasnet': base_model = NASNetLarge(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='tf' elif args.model == 'resnet': base_model = ResNet50(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='caffe' elif args.model == 'vgg16': base_model = VGG16(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='caffe' elif args.model == 'vgg19': base_model = VGG19(include_top=False, weights='imagenet', pooling=args.pooling) model = Model(input=base_model.input, output=base_model.get_layer(args.output_layer).output) preprocess_mode='caffe' else: print ("Model not found") return 0 return model,preprocess_mode
outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('flatten').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('mixed9').output) image_size = (299, 299) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None # In[ ]: # path to training dataset train_labels = sorted(os.listdir(train_path)) # variables to hold features and labels features = [] labels = [] label = 0 i = 0
image_size = (224, 224) elif model_name == "vgg19": base_model = VGG19(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('flatten').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('mixed9').output) image_size = (299, 299) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None # In[10]: # loop over all the labels in the folder for label in train_labels: cur_path = train_path + "/" + label for image_path in glob.glob(cur_path): img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0)