コード例 #1
0
    ##  https://github.com/soeaver/caffe-model/blob/master/cls/evaluation_cls.py
    #   https://github.com/soeaver/caffe-model/blob/master/cls/synset.txt
    #    (441)  810 n02823750 beer glass
    #    (  1)  449 n01443537 goldfish, Carassius auratus
    #    (  9)  384 n01518878 ostrich, Struthio camelus
    img_file = '/root/share/data/imagenet/dummy/256x256/goldfish.jpg'
    img_file = '/root/share/data/imagenet/dummy/256x256/beer_glass.jpg'
    img_file = '/root/share/data/imagenet/dummy/256x256/ostrich.jpg'
    img = image.load_img(img_file, target_size=(299, 299))
    #img = np.ones((299,299,3),np.uint8)

    keras_model = KXception(include_top=True, weights='imagenet', input_tensor=None)
    xx = image.img_to_array(img)
    xx = np.expand_dims(xx, axis=0)
    xx = preprocess_input(xx)
    preds = keras_model.predict(xx)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('Predicted:', decode_predictions(preds, top=3)[0])




    ## dump keras weights ###############################################

    keras_weights = dict()
    for layer in keras_model.layers:   # dump all weights (trainable and not) to dict {layer_name: layer_weights}
        for layer, layer_weights in zip(layer.weights, layer.get_weights()):
            keras_weights[layer.name] = layer_weights

コード例 #2
0
def train(epochs):
    image_size = (299, 299)
    # variables to hold features and labels
    features = []
    labels = []

    # default setting in keras models
    class_count = 1000
    X_test = []
    name_test = []

    trainData = np.loadtxt("./train.txt", dtype="str", delimiter=' ')
    for k in range(len(trainData)):
        aLine = trainData[k]
        image_path = aLine[0]
        label = int(aLine[1])
        ground_truth = np.zeros(class_count, dtype=np.float32)
        ground_truth[label] = 1

        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        labels.append(ground_truth)
        features.append(x[0])

    trainData = np.loadtxt("./val.txt", dtype="str", delimiter=' ')
    for k in range(len(trainData)):
        aLine = trainData[k]
        image_path = aLine[0]
        label = int(aLine[1])
        ground_truth = np.zeros(class_count, dtype=np.float32)
        ground_truth[label] = 1

        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        labels.append(ground_truth)
        features.append(x[0])

    testData = np.loadtxt("./test.txt", dtype="str", delimiter=' ')
    for k in range(len(testData)):
        aLine = testData[k]
        image_path = aLine
        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        X_test.append(x[0])
        name_test.append(image_path)

    X_train = features
    y_train = labels

    X_train = np.array(X_train)
    Y_train = np.array(y_train)

    # test image
    X_test = np.array(X_test)

    # Use Xception
    model = Xception(include_top=True, weights='imagenet', classes=class_count)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.fit(X_train, Y_train, epochs=epochs, verbose=1, validation_split=0.3)

    Y_pred = model.predict(X_test)

    f = open('project2_08573584.txt', 'w')
    for i in range(len(name_test)):
        predict = Y_pred[i].argmax(axis=0)
        f.write(str(predict) + '\n')
    f.close()
コード例 #3
0
class Xception:
    def __init__(self):
        self.input_shape = (299, 299, 3)
        self.weight = 'imagenet'
        self.pooling = 'avg'
        self.load_config()

    def load_config(self):
        # read model config from environment
        self.device_str = os.environ.get("device_id", "/cpu:0")
        self.user_config = tf.ConfigProto(allow_soft_placement=False)
        gpu_mem_limit = float(os.environ.get("gpu_mem_limit", 0.3))
        self.user_config.gpu_options.per_process_gpu_memory_fraction = gpu_mem_limit
        self.user_config.gpu_options.allow_growth = True
        if os.environ.get("log_device_placement", False):
            self.user_config.log_device_placement = True
        print("device id %s, gpu memory limit: %f" %
              (self.device_str, gpu_mem_limit))

        self.graph = tf.Graph()
        with self.graph.as_default():
            with tf.device(self.device_str):
                self.session = tf.Session(config=self.user_config)
                KTF.set_session(self.session)
                self.model = KerasXception(weights=self.weight,
                                           input_shape=(self.input_shape[0],
                                                        self.input_shape[1],
                                                        self.input_shape[2]),
                                           pooling=self.pooling,
                                           include_top=False)
                self.graph = KTF.get_graph()
                self.session = KTF.get_session()
                self.model.trainable = False
                self.model.predict(
                    np.zeros((1, self.input_shape[0], self.input_shape[1], 3)))

    def extract_feature(self, img_path):
        img = image.load_img(img_path,
                             target_size=(self.input_shape[0],
                                          self.input_shape[1]))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img = preprocess_input_xception(img)
        with self.graph.as_default():
            with tf.device(self.device_str):
                with self.session.as_default():
                    feat = self.model.predict(img)

        norm_feat = feat[0] / LA.norm(feat[0])
        norm_feat = [i.item() for i in norm_feat]
        return norm_feat

    @property
    def name(self):
        return "xception"

    @property
    def type(self):
        return "encoder"

    @property
    def input(self):
        return "image"

    @property
    def output(self):
        return "vector"

    @property
    def dimension(self):
        return "2048"

    @property
    def metric_type(self):
        return "L2"
コード例 #4
0
                       metrics=['acc'])

with open('Finetuned_DXception_finale_7.json', 'w') as f:  # save the model
    f.write(cas_classifier.to_json())

checkpointer = ModelCheckpoint(
    filepath='Finetuned_DXception_finale_7.hdf5',
    verbose=1,
    save_best_only=False,
    mode='min'
)  #saves the model weights after each epoch if the validation loss decreased

(val_noisy, val_labels) = validation_data_generation(Val_list, Val_size)
val_labels = to_categorical(val_labels, num_classes=1000)

NPredicted_Labels = np.argmax(classifier.predict(val_noisy), 1)
Noisy_accuracy = np.mean(
    np.equal(NPredicted_Labels, np.argmax(val_labels, axis=1)))
print('Noisy accuracy on Xception before fine-tunning:' + str(Noisy_accuracy))

#val_denoised=De_model.predict(val_noisy, verbose=1,batch_size=batch_size)
NPredicted_Labels = np.argmax(cas_classifier.predict(val_noisy), 1)
Noisy_accuracy = np.mean(
    np.equal(NPredicted_Labels, np.argmax(val_labels, axis=1)))
print('Noisy accuracy on cascaded_Xception before fine-tunning:' +
      str(Noisy_accuracy))

for i in range(5):

    print('%.i-th Training data loading...' % (i + 1))
コード例 #5
0
class TestSetAnalysis(object):
	"""
	class string
	"""

	def __init__(self, model='vgg19', show=True):
		"""
		doc string constructor
		"""
		firstlayer_index = 0
		if model=='vgg19':
		    from keras.applications.vgg19 import VGG19
		    self.model = VGG19(weights='imagenet', include_top = True)
		elif model=='vgg16':
		    from keras.applications.vgg16 import VGG16
		    self.model = VGG16(weights='imagenet', include_top = True)
		elif model=='inceptionv3':
			from keras.applications.inception_v3 import InceptionV3
			self.model = InceptionV3(weights='imagenet', include_top = True)
		elif model=='resnet50':
		    from keras.applications.resnet50 import ResNet50
		    self.model = ResNet50(weights='imagenet', include_top = True)
		elif model=='xception':
		    from keras.applications.xception import Xception
		    self.model = Xception(weights='imagenet', include_top = True)        
		elif model.endswith('.hdf5'):
			self.model = load_model(model)
			firstlayer_index = 1
		else:
		    print("Valid models are:")
		    print("vgg19, vgg16, inceptionv3, resnet50, xception")
		    print("xception/inceptionv3 model is only available in tf backend")
		    print("Or provide path to a saved model in .hdf5 format")
		    exit()
		if show:
			print(self.model.summary())
		self.inputshape = self.model.layers[firstlayer_index].output_shape[1:]

#------------------------------------------------------------------------------

	def predict_gen(self, data_dir, batchsize=32, rescale=1.0/255):
		self.data_dir = data_dir
		datagen = ImageDataGenerator(rescale=rescale)
		self.generator = datagen.flow_from_directory(self.data_dir, \
								target_size=self.inputshape[:2], \
		                        batch_size=batchsize, \
		                        class_mode='categorical', \
		                        shuffle=False)

		nfiles = []
		class_folders = glob(self.data_dir+'*')
		for i in range(len(class_folders)):
		    files = glob(class_folders[i]+'/*')
		    nfiles.append(len(files))

		samples = self.generator.samples
		self.nb_class = self.generator.num_class
		self.predictions = self.model.predict_generator(self.generator, \
												samples/batchsize+1)
		self.predictions = self.predictions[:samples, :]
		self.predict_labels = np.argmax(self.predictions, axis=1)
		self.true_labels = []
		for i in range(self.nb_class):
			self.true_labels +=  list([i] * nfiles[i])

		self.confusion_matrix = confusion_matrix(\
										self.true_labels, \
										self.predict_labels)

		if self.nb_class==2:
			self.FPR, self.TPR, thresholds = roc_curve(\
										self.true_labels, \
										self.predictions[:,1])
			self.roc_auc = roc_auc_score(\
										self.true_labels, \
										self.predictions[:,1])
			self.get_cm_index()

#------------------------------------------------------------------------------

	def predict_array(self, xdata, ydata, batchsize=32, rescale=1.0/255):

		# samples = self.generator.samples
		# self.nb_class = self.generator.num_class
		self.predictions = self.model.predict(xdata*rescale, batch_size=batchsize)
		# self.predictions = self.predictions[:samples, :]
		self.predict_labels = np.argmax(self.predictions, axis=1)
		self.true_labels = np.argmax(ydata, axis=1)

		self.confusion_matrix = confusion_matrix(\
										self.true_labels, \
										self.predict_labels)

		self.FPR, self.TPR, thresholds = roc_curve(\
									self.true_labels, \
									self.predictions[:,1])
		self.roc_auc = roc_auc_score(\
									self.true_labels, \
									self.predictions[:,1])
		self.get_cm_index()			

#------------------------------------------------------------------------------

	def get_information_dictionary(self):
		mydict = {
			"FPR": self.FPR,
			"TPR": self.TPR,
			"predictions": self.predictions,
			"true_labels": self.true_labels,
			"predict_labels": self.predict_labels,
			"roc_auc": self.roc_auc,
			"confusion_matrix": self.confusion_matrix
		}
		return mydict

#------------------------------------------------------------------------------

	def plot_confusion_matrix(self, cmap='Blues', \
								save=False, savename='cm.png'):
		plt.figure(figsize=(8,8))
		matrix = np.zeros(self.confusion_matrix.shape)
		for i in range(len(matrix)):
			matrix[i] = self.confusion_matrix[i]/\
						float(np.sum(self.confusion_matrix[i]))
		plt.imshow(matrix, cmap=cmap)
		plt.xticks([], [])
		plt.yticks([], [])
		plt.clim(0, 1)
		if save:
			print("Now saving confusion matrix figure")
			plt.savefig(savename)
		else:
			plt.show()
		return matrix

#------------------------------------------------------------------------------

	def plot_roc_curve(self, \
					save=False, savename='roc.png'):
		plt.figure(figsize=(8,8))
		plt.plot(self.FPR, self.TPR, 'k', lw=2)
		plt.plot(self.FPR, self.FPR, 'k', lw=0.5)
		plt.axhline(y=1, color='k', ls=':', lw=0.5)
		plt.axvline(x=0, color='k', ls=':', lw=0.5)
		plt.xlim(-0.01,1)
		plt.ylim(0,1.01)
		plt.xlabel('$\mathtt{FalsePositiveRate}$', fontsize=22)
		plt.ylabel('$\mathtt{TruePositiveRate}$', fontsize=22)
		if save:
			f.savefig(savefigname)
		else:
			plt.show()

#------------------------------------------------------------------------------

	def plot_samples(self, ind_arr, title, N=100, ncol=15, \
						save=False, savefigname='samples.eps'):

	    ind_arr = np.random.choice(ind_arr, size=N, replace=False)
	    names = np.array(self.generator.filenames)[ind_arr]
	    N = N - N%ncol
	    print(N)
	    nrow = N/ncol
	    f, axarr = plt.subplots(nrow, ncol, sharex=True, sharey=True, \
	    						figsize=(ncol, nrow))
	    f.subplots_adjust(wspace=0.0, hspace=0)
	    f.suptitle("$\mathtt{%s}$"%title, fontsize=22)

	    for i in range(nrow):
	        for j in range(ncol):
	            axarr[i,j].imshow(cv2.imread(self.data_dir+names[i*ncol+j]))
	            axarr[i,j].set_xticks([], [])
	            axarr[i,j].set_yticks([], [])
	    if save:
	    	f.savefig(savefigname)
	    else:
	    	plt.show()

#------------------------------------------------------------------------------

	def get_cm_index(self):
	    self.tp = []
	    self.tn = []
	    self.fp = []
	    self.fn = []
	    for i in range(len(self.true_labels)):
	        if self.true_labels[i]==1 and self.predict_labels[i]==1:
	            self.tp.append(i)
	        elif self.true_labels[i]==0 and self.predict_labels[i]==0:
	            self.tn.append(i)
	        elif self.true_labels[i]==0 and self.predict_labels[i]==1:
	            self.fp.append(i)
	        elif self.true_labels[i]==1 and self.predict_labels[i]==0:
	            self.fn.append(i)

#------------------------------------------------------------------------------

	def plot_all(self):
		self.plot_confusion_matrix()
		if self.nb_class==2:
			self.plot_roc_curve()
			self.plot_samples(self.tp, 'TruePositive')
			self.plot_samples(self.fp, 'FalsePositive')
			self.plot_samples(self.tn, 'TrueNegative')
			self.plot_samples(self.fn, 'FalseNegative')
コード例 #6
0
class image_analysis(object):
	"""
	class string
	"""

	def __init__(self, model='vgg19'):
		"""
		doc string constructor
		"""
		firstlayer_index = 0
		if model=='vgg19':
		    from keras.applications.vgg19 import VGG19
		    self.model = VGG19(weights='imagenet', include_top = True)
		elif model=='vgg16':
		    from keras.applications.vgg16 import VGG16
		    self.model = VGG16(weights='imagenet', include_top = True)
		elif model=='inceptionv3':
			from keras.applications.inception_v3 import InceptionV3
			self.model = InceptionV3(weights='imagenet', include_top = True)
		elif model=='resnet50':
		    from keras.applications.resnet50 import ResNet50
		    self.model = ResNet50(weights='imagenet', include_top = True)
		elif model=='xception':
		    from keras.applications.xception import Xception
		    self.model = Xception(weights='imagenet', include_top = True)        
		elif model.endswith('.hdf5'):
			self.model = load_model(model)
			firstlayer_index = 1
		else:
		    print("Valid models are:")
		    print("vgg19, vgg16, inceptionv3, resnet50, xception")
		    print("xception/inceptionv3 model is only available in tf backend")
		    print("Or provide path to a saved model in .hdf5 format")
		    exit()
		self.inputshape = self.model.layers[firstlayer_index].output_shape[1:]

#------------------------------------------------------------------------------

	def get_labels(self, path):
		"""
		my doc string
		"""
		labels = []
		f = open(path, 'r')
		while True:
			line = f.readline()
			if not line:
				break
			labels.append(line)
		f.close()
		return labels

#------------------------------------------------------------------------------

	def get_image_input(self, imagepath):
		"""
		my doc string
		"""		
		shape = self.inputshape[:2]
		im = cv2.resize(cv2.imread(imagepath), shape).astype(np.float32)
		im[:,:,0] -= 103.939
		im[:,:,1] -= 116.779
		im[:,:,2] -= 123.68
		im = np.expand_dims(im, axis=0)
		return im

#------------------------------------------------------------------------------

	def get_image_category(self, imagepath=None, labelpath=None, k=5):
		if imagepath==None or labelpath==None:
			print("Usage: predict_image(imagepage, labelpath)")
			exit()
		im = self.get_image_input(imagepath)
		out = self.model.predict(im)
		idxs = np.argsort(out[0])[::-1][:k]
		labels = self.get_labels(labelpath)
		results = np.array(labels, dtype='str')[idxs]
		probs = np.array(out[0,idxs], dtype='float')
		dictionary = {}
		for i in range(k):
			dictionary[results[i].replace('\n','')] = probs[i]
		return dictionary

#------------------------------------------------------------------------------

	def get_features(self, img_path, layername='fc1'):
		model = Model(inputs=self.model.input, \
				outputs=self.model.get_layer(layername).output)
		im = self.get_image_input(img_path)
		features = model.predict(im)
		return features

#------------------------------------------------------------------------------

	def get_features_vector(self, img_path, layername='fc1'):
		features = self.get_features(img_path, layername)
		return np.ndarray.flatten(features)

#------------------------------------------------------------------------------

	def get_layernames(self):
	    allnames = []
	    for i in range(len(self.model.layers)):
	        names = self.model.layers[i].name
	        allnames.append(names)
	    return allnames		

#------------------------------------------------------------------------------

	def plot_conv_features(self, image, layer=None, \
						save=False, savefilename='features.eps'):
		if layer==None:
			names = self.get_layernames()
			for i in range(len(names)):
				if 'conv' in names[i]:
					layer=names[i]
					break
		elif type(layer)==int:
			names = self.get_layernames()
			layer = names[layer]

		if not (('conv' in layer) or ('pool' in layer)):
			print("please provide name or index of convolution/pooling layer")
			exit()

		features = self.get_features(image, layer)
		N = features.shape[-1]
		nrow = int(N**0.5)
		ncol = int(N**0.5)

		print("=================================================")
		print("Layer name: ", layer)
		print("Features shape: ", features.shape)
		print("Number of rows and columns: ", nrow, ncol)
		print("=================================================")

		f, axarr = plt.subplots(nrow, ncol, \
	    	sharex=True, sharey=True, figsize=(20,20))
		# f.suptitle('$\mathtt{%s}$'%layer.replace('_', " "), fontsize=22)
		f.subplots_adjust(wspace=0.02, hspace=0.02)
		for i in range(nrow):
		    for j in range(ncol):
		        axarr[i,j].imshow(features[0,:,:,i*ncol+j], cmap='jet')
		        axarr[i,j].set_xticks([], [])
		        axarr[i,j].set_yticks([], [])
		if save:
			f.savefig(savefilename)
		else:
			plt.show()

#------------------------------------------------------------------------------

	def plot_all_conv_features(self, image, DIR='figures/'):
		if not os.path.exists(DIR):
			os.mkdir(DIR)
		names = self.get_layernames()
		for i in range(len(names)):
		    if ('conv' in names[i]) or ('pool' in names[i]):
		    	filename = DIR+names[i]+'.eps'
		        self.plot_conv_features(image, names[i],save=True, savefilename=filename)
コード例 #7
0
    # Load our model
    # model = densenet169_model(img_rows=img_rows, img_cols=img_cols, color_type=channel, num_classes=num_classes)

    # load keras model
    model = Xception(weights=None, classes=10)
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()

    # Start Fine-tuning
    model.fit(
        X_train,
        Y_train,
        batch_size=batch_size,
        epochs=nb_epoch,
        shuffle=True,
        verbose=1,
        validation_data=(X_valid, Y_valid),
    )

    # Make predictions
    predictions_valid = model.predict(X_valid,
                                      batch_size=batch_size,
                                      verbose=1)

    # Cross-entropy loss score
    score = log_loss(Y_valid, predictions_valid)
コード例 #8
0
# In[ ]:

from keras.models import load_model

#model = load_model('/content/drive/My Drive/ColabNotebooks/AllmodeloRMSpropXception.h5')

model = load_model(
    '/content/drive/My Drive/ColabNotebooks/Xception/modelXception.h5')

# ## Predict the Model Trained

# **Show the three better images and the three wrong image of the test set**

# In[ ]:

predictTest = model.predict(x_test, verbose=1)
predictTest = predictTest.reshape(predictTest.shape[0])
#predictTest = predictTest.astype('int32')
#print(x_test.shape)
#print(predictTest.shape)
#print(y_test.shape)
#print(np.round(predictTest))
#print(y_test)
#print(predictTest)

mae = np.abs(y_test - predictTest)
#print(mae)
pos = np.argsort(mae)

print(pos[-1])
print(pos[-2])
コード例 #9
0
from keras.applications.xception import Xception
from keras.preprocessing import image
from keras.applications.xception import preprocess_input, decode_predictions
from PIL import Image
import numpy as np
import os
import time

model = Xception(weights='imagenet')

images = os.listdir('resources')
times = []

for img_path in images:
    img = image.load_img('resources/' + img_path, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    start_time = time.time()
    preds = model.predict(x)
    times.append(time.time() - start_time)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('Predicted for ' + img_path + ": ", decode_predictions(preds, top=3)[0])

print("Average prediction time: %s seconds" % np.mean(times))
コード例 #10
0
# load the input image using the Keras helper utility while ensuring
# that the image is resized to 224x224 pxiels, the required input
# dimensions for the network -- then convert the PIL image to a
# NumPy array
print("[INFO] loading and preprocessing image...")
image = image_utils.load_img(args["image"], target_size=(224, 224))
image = image_utils.img_to_array(image)

# our image is now represented by a NumPy array of shape (3, 224, 224),
# but we need to expand the dimensions to be (1, 3, 224, 224) so we can
# pass it through the network -- we'll also preprocess the image by
# subtracting the mean RGB pixel intensity from the ImageNet dataset
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)

# load the VGG16 network
print("[INFO] loading network...")
model = Xception(weights="imagenet")

# classify the image
print("[INFO] classifying image...")
preds = model.predict(image)
label = decode_predictions(preds, top=3)[0]
print('Predicted:', label)

# display the predictions to our screen
cv2.putText(orig, "Label: {}".format(label), (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0)
コード例 #11
0
class FaceEmotion:
    """Class for recognizing emotion using default Deep Learning Xception model"""
    def __init__(self, input_shape=(200, 200, 3)):  # TODO: Check input_shape
        """Initialize main parameters of FaceEmotion class
        :param input_shape: Input images shape
        """
        self.input_shape = input_shape

        self.model = Xception(include_top=False, input_shape=input_shape)
        self.model = self.add_classificator(self.model)

    @staticmethod
    def add_classificator(base_model):
        """Add a classificator to a model
        :param base_model: Keras model object
        """
        layer = base_model.output
        layer = GlobalAveragePooling2D(name="classificator_block_pool")(layer)
        layer = Dense(512,
                      activation='relu',
                      name='classificator_block_dense_1')(layer)
        layer = Dense(64,
                      activation='relu',
                      name='classificator_block_dense_2')(layer)
        layer = Dense(6, activation='relu',
                      name='classificator_block_dense_3')(layer)

        model = Model(inputs=base_model.input, outputs=layer)

        # freeze early layers
        for l in base_model.layers:
            l.trainable = False

        model.compile(optimizer='sgd',
                      loss='mean_squared_error',
                      metrics=['accuracy'])

        return model

    def model_architecture(self, filename=None):
        """Show model architecture and save it to file
        :param filename: Path to the model architecture image file
        """
        list_summary = []
        self.model.summary(print_fn=lambda x: list_summary.append(x))
        summary = "\n".join(list_summary)

        if filename:
            with open(filename + '.txt', 'w') as f:
                f.write(summary)

            from keras.utils import plot_model
            plot_model(self.model, filename + '.jpg')

        return summary

    # noinspection PyShadowingNames
    def train(self, generator, epochs, steps_per_epoch):
        """Train model
        :param generator: Data generator compatible with Keras model
        :param epochs: Number of epochs to train model
        :param steps_per_epoch: Number of faces used in one step
        """
        stopper = EarlyStopping(patience=100)  # , restore_best_weights=True)
        save_dir = "training/e{epoch:02d}-a{acc:.2f}.ckpt"
        saver = ModelCheckpoint(save_dir)  # , save_best_only=True)

        self.model.fit_generator(generator,
                                 steps_per_epoch,
                                 epochs,
                                 callbacks=[stopper, saver])

    def get_emotion(self, image):
        emotions = []
        for top, right, bottom, left in face_locations(image):
            emotion = self.model.predict(image[top:bottom, left:right])
            emotions.append(emotion)
        return emotions
コード例 #12
0
plt.subplots(figsize=(10,10))
plt.tight_layout()
display_training_curves(history1.history['accuracy'], history1.history['val_accuracy'], 'accuracy', 211)
display_training_curves(history1.history['loss'], history1.history['val_loss'], 'loss', 212)

n1=224
m1=224
features1 = []
for image in x_train:

  image = cv2.resize(image , (n1,m1) )

  image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  image = preprocess_input(image)

  feature = res.predict(image)
  
  features1.append(feature)

featurestest1 = []
for image in x_test:

  image = cv2.resize(image , (n1,m1) )

  image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  image = preprocess_input(image)

  feature = res.predict(image)
  
  featurestest1.append(feature)
features = np.array(features1)
コード例 #13
0
#____________________________________________ GRAD CAM RESNET50______________________________________________


def normalize(x):
    # utility function to normalize a tensor by its L2 norm
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)


model = Xception()
plot_model(model, to_file='model/xception.png')
image = load_img(input_images_dir + '/input.jpg', target_size=(229, 229))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)

predictions = model.predict(image)
predicted_class = np.argmax(predictions)


def target_category_loss(x, category_index, nb_classes):
    return tf.multiply(x, K.one_hot([category_index], nb_classes))


def target_category_loss_output_shape(input_shape):
    return input_shape


activation_layer = 'block14_sepconv2_act'
nb_classes = model.get_layer('predictions').output.shape[
    1]  # get number of classes
target_layer = lambda x: target_category_loss(x, predicted_class, nb_classes)
コード例 #14
0
net = Dropout(0.34)(Xception_branch)
net = Dense(1024, use_bias=False, kernel_initializer='uniform')(net)
net = BatchNormalization()(net)
net = Activation("relu")(net)
net = Dropout(0.34)(net)
net = Dense(120, kernel_initializer='uniform', activation="softmax")(net)

model = Model(inputs=[Xception_input], outputs=[net])
model.summary()

model.load_weights('2019-03-18_dog_breed_model.h5')

name_f = np.load('names.npy')

X_validXception = np.zeros((1, 10, 10, 2048), dtype=np.float32)

img = load_img('chien.jpg', target_size=(299, 299))  # this is a PIL image

x_img = img_to_array(img)
x_img = x_img.reshape((1, ) + x_img.shape)

imput_train_xception = preprocess_input_Xception(x_img.copy())
train_xception = model_xception.predict(
    imput_train_xception)  # ,batch_size=32)

X_validXception[0, :, :, :] = train_xception[0, :, :, :]

predictions = model.predict([X_validXception])

print('voici la race du chien prédite par notre algorithme : {}'.format(
    name_f[int(np.argmax(predictions))]))
コード例 #15
0
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.xception import preprocess_input
from keras.applications.xception import decode_predictions
from keras.applications.xception import Xception

model = Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)
for i in range(0, 5):
    print('-----------------------------------------------')
    # load an image from file
    image = load_img('../resources/wolf_'+str(i+1)+'.jpg', 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], image.shape[2]))
    # prepare the image for the VGG model
    image = preprocess_input(image)
    # predict the probability across all output classes
    yhat = model.predict(image)
    # convert the probabilities to class labels
    label = decode_predictions(yhat)
    for i in range(0, len(label[0])):
        # retrieve the most likely result, e.g. highest probability
        labelTemp = label[0][i]
        # print the classification
        print('%s (%.2f%%)\n' % (labelTemp[1], labelTemp[2]*100))
for i in range(20):
    model = Xception(include_top=True,
                     weights='weights_Xception_ten_res{}.hdf5'.format(i),
                     input_tensor=None,
                     input_shape=input_shape,
                     pooling=None,
                     classes=10)

    Adam(lr=0.005,
         beta_1=0.9,
         beta_2=0.999,
         epsilon=None,
         decay=0.0,
         amsgrad=False)
    model.compile(optimizer='Adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    result_class += model.predict(x_test)

result = np.array(np.argmax(result_class, axis=1),
                  'int').reshape(x_test.shape[0], 1)
result_id = np.arange(0, x_test.shape[0]).reshape(-1, 1)
resultt = np.concatenate((result_id, result), axis=1)
np.savetxt("xception_ten.csv",
           resultt,
           fmt='%i',
           header='Id,Category',
           delimiter=',',
           comments='')
コード例 #17
0
import numpy as np
print("Module imports successful.")



# import images
# full_path = ""~/CS/Hydroponic-Root-Classifier/"

test_image = cv.imread("allData/1_a.jpg")
print(test_image.shape)

preprocess_input(test_image) # doesn't change input dimensions


# build pre-trained xception model
model = Xception(include_top=False,
                 weights='imagenet',
                 input_tensor=None,
                 input_shape=(256, 256, 3),
                 pooling=None)

# expand x to the 4-dimensional tensor (batch_size, image_width, image_height, channels)
# required by the Xception model as input
test_image = np.expand_dims(test_image, axis=0)


# use xception model to make prediction for images
predictions = model.predict(test_image, batch_size=1, verbose=0, steps=None)
print("Prediction completed.")
print(predictions.shape)