示例#1
0
 def create_xception_model(self):
     #vgg16_model = VGG16(include_top=self.vgg16_include_top, weights='imagenet')#包含最上层的连接层,imagenet表示加载imagenet与训练的网络权重.
     xception_model=Xception(include_top=True, weights='imagenet', input_tensor=None)
     xception_model.compile(optimizer=SGD(), loss='categorical_crossentropy', metrics=['accuracy'])
     xception_model.summary()
     plot_model(xception_model, to_file='xception_model.png', show_shapes=True)
     return xception_model
示例#2
0
def extract_features(directory):
    dir1=directory
    #extracting features
    model=Xception()
    #removing the last output layer
    model.layers.pop()
    #defining the Model which will take the input the size of the imahe and output the values in the last layer
    model=Model(inputs=model.inputs,output=model.layers[-1].output)
    
    print(model.summary())
    features=dict()
    file=os.listdir(dir1)
    for name in file:
        path=os.path.join(dir1,name)
        image=load_img(path=path,target_size=(299,299))
        #conv to np array
        image=img_to_array(image)
        image=image.reshape((1,image.shape[0],image.shape[1],image.shape[2]))
        #preprocessing the input image according to the Xception model
        image=preprocess_input(image)
        feature=model.predict(image)    
        #get  the image id
        image_id=name.split('.')[0]
        features[image_id]=feature
        print('>%s'%name)
    return features
示例#3
0
def create_model(input_shape, config):

    input_tensor = Input(shape=input_shape)  # this assumes K.image_dim_ordering() == 'tf'
    xception_model = Xception(include_top=False, weights=None, input_tensor=input_tensor)
    print(xception_model.summary())

    x = xception_model.output
    x = GlobalAveragePooling2D()(x)
    predictions = Dense(config["num_classes"], activation='softmax')(x)

    return Model(input=xception_model.input, output=predictions)
def Xception_Yolo(input_shape=(240, 320, 3)):
    xception = Xception(weights='imagenet',
                        include_top=False,
                        input_shape=input_shape)
    xception.summary()
    img_input = Input(shape=input_shape)
    yolo = xception(img_input)

    yolo = Conv2D(128, [1, 1])(yolo)
    yolo = BatchNormalization(name='norm_1')(yolo)
    yolo = LeakyReLU(alpha=0.1)(yolo)

    yolo = Flatten()(yolo)
    yolo = Dense(1024)(yolo)
    yolo = BatchNormalization()(yolo)
    yolo = LeakyReLU(alpha=0.1)(yolo)
    yolo = Dense(576)(yolo)
    yolo = Reshape((6, 8, 12))(yolo)
    model = Model(img_input, yolo)
    model.summary()
    return model
示例#5
0
def load_model(model):
    K.clear_session()
    if model == "ResNet152":
        base_model = ResNet152(include_top=False,
                               weights='imagenet',
                               input_shape=(299, 299, 3),
                               pooling="max")
    if model == "InceptionV3":
        base_model = InceptionV3(include_top=False,
                                 weights='imagenet',
                                 input_shape=(299, 299, 3),
                                 pooling="max")
    if model == "InceptionResNetV2":
        base_model = InceptionResNetV2(include_top=False,
                                       weights='imagenet',
                                       input_shape=(299, 299, 3),
                                       pooling="max")
    if model == "Xception":
        base_model = Xception(include_top=False,
                              weights='imagenet',
                              input_shape=(299, 299, 3),
                              pooling="max")
    if model == "VGG":
        base_model = VGG19(include_top=False,
                           weights='imagenet',
                           input_shape=(299, 299, 3),
                           pooling="max")
    base_model.summary()
    x = base_model.output
    dense1_ = Dense(512, activation='relu')
    dense1 = dense1_(x)
    x = Dropout(0.2)(dense1)
    dense2 = Dense(256, activation='relu')(x)
    x = Dropout(0.2)(dense2)
    dense3 = Dense(128, activation='relu')(x)
    pred_output = Dense(1, activation='sigmoid')(dense3)
    model = Model(inputs=base_model.input, outputs=[pred_output])
    model.summary()
    return model
示例#6
0
    def buildModel(self):

        # 获取预训练的卷基层
        Xinceptionv = Xception(weights='imagenet', include_top=False, pooling="avg")
        Xinceptionv.summary()

        #plot_model(Xinceptionv, to_file='../data/model.png', show_shapes=True)

        # 输入层
        input = Input(shape=(self.imgSize, self.imgSize, 3), name = 'image_input')

        # 预训练的卷基层
        output_Xinceptionv = Xinceptionv(input)


        # 加入的全连接层
        x = output_Xinceptionv
        # x1 = GlobalAveragePooling2D()(x)
        # x2 = GlobalMaxPooling2D()(x)
        # x = concatenate([x1, x2])

        #x = Flatten(name='flatten')(output_resnet50)
        #x = Dense(1024, activation="relu", name="fc1")(x)
        #x = Dropout(0.5)(x)
        #x = Dense(512, activation="relu", name="fc1")(x) # 此处改成avgpool,只用一层,去掉dropout
        #x = Dropout(0.5)(x)

        x = Dense(100, kernel_regularizer=regularizers.l2(0.01), activation='softmax')(x)

        #model_resnet50.trainabel = False # 冻结全部resnet的卷积权重

        # for layer in Xinceptionv.layers: # 冻结前面的卷积层
        #     layer.trainable = False

        # 建模
        my_model = Model(input=input, output=x)

        return my_model
class Model:
    def __init__(self, weight_decay=None):
        """
        # The range of labels (steeting) in good driving condition goes from -1 to 1. Now this could be a good use
        case for tanh.
        """
        self.base_model = Xception(
            weights='imagenet', input_shape=(90, 320, 3), include_top=False
        )  # imports the mobilenet model and discards the last 1000
        # neuron layer.
        if weight_decay is not None:
            self.add_weight_decay(weight_decay)
        self.crop = Cropping2D(cropping=((50, 20), (0, 0)),
                               input_shape=(160, 320, 3))
        self.normalize = keras.layers.Lambda(lambda x: (x / 255.) - 0.5)
        self.flatten = keras.layers.Flatten()
        self.dropout = keras.layers.Dropout(0.5)
        self.dense_squash = keras.layers.Dense(1, activation="tanh")

    def __call__(self):
        inputs = keras.layers.Input(shape=(160, 320, 3))
        inputs_pp = self.crop(inputs)
        inputs_pp = self.normalize(inputs_pp)
        inputs_pp = self.base_model(inputs_pp)
        inputs_pp = self.flatten(inputs_pp)
        # inputs_pp = self.dropout(inputs_pp)
        outlayer = self.dense_squash(inputs_pp)

        model = keras.Model(inputs=[inputs],
                            outputs=[outlayer],
                            name="xception_augmented")
        print(self.base_model.summary())
        return model

    def add_weight_decay(self, decay_val):
        for layer in self.base_model.layers:
            print("layer: ", layer)
            if isinstance(layer, keras.layers.Conv2D) or isinstance(
                    layer, keras.layers.Dense):
                layer.add_loss(lambda: keras.regularizers.l2(decay_val)
                               (layer.kernel))
            if hasattr(layer, 'bias_regularizer') and layer.use_bias:
                layer.add_loss(lambda: keras.regularizers.l2(decay_val)
                               (layer.bias))
示例#8
0
def Xception_model():
    input_tensor = Input(shape=(dimension, dimension, number_of_channels))
    model = Xception(input_tensor=input_tensor,
                     weights='imagenet',
                     include_top=True)
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    x = Dense(number_of_classes, activation='softmax')(model.output)
    model = Model(model.input, x)

    # the first 24 layers are not trained
    for layer in model.layers[:24]:
        layer.trainable = False

    lrate = 0.001
    decay = 0.000001
    adam = Adam(lr=lrate, decay=decay)
    model.compile(loss='categorical_crossentropy',
                  optimizer=adam,
                  metrics=['accuracy'])

    print(model.summary())
    return model
    cam = np.dot(output, weights)

    cam = cv2.resize(cam, (299, 299), cv2.INTER_LINEAR)
    cam = np.maximum(cam, 0)
    cam = cam / cam.max()

    jetcam = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
    jetcam = cv2.cvtColor(jetcam, cv2.COLOR_BGR2RGB)
    jetcam = (np.float32(jetcam) + x / 2)

    return jetcam


if __name__ == '__main__':
    model = Xception(include_top=False, weights='imagenet', pooling='avg')
    model.summary()
    all_path = glob.glob('./images/*')
    layer_name = [l.name for l in model.layers]
    lay = 'global_average_pooling2d_1'
    #pprint.pprint(layer_name)
    #os.makedirs('./sample', exist_ok=True)
    for i, path in enumerate(all_path):
        path_result = os.path.join(path, 'grad_result2')
        print(path)
        os.makedirs(path_result, exist_ok=True)
        img_path = glob.glob(path + '/*.png')
        for l, img in enumerate(img_path):
            x = img_to_array(load_img(img, target_size=(299, 299)))
            image = Grad_Cam(model, x, lay)
            array_to_img(image)
            out_path = os.path.join(path_result, 'output_{0}.png'.format(l))
x = BatchNormalization(name='new_batch_2')(x)
x = Dropout(0.3,name='new_drop_2')(x)


prediction = Dense(3,activation='softmax',name='new_dene_2')(x)
model_1 = Model(inputs=inception_model_1.input,outputs=prediction)

layers = ['new_pool_1','new_batch_1','new_dense_1','new_batch_2','new_drop_2','new_dene_2']
for layer in layers:
    lr_dict[layer] = 0.0002*10

!pip install keras-lr-multiplier

from keras_lr_multiplier import LRMultiplier

model_1.summary()

model_1.compile(loss='categorical_crossentropy',optimizer=LRMultiplier('adam', lr_dict),metrics=['accuracy'])

train_set_1 = preprocess_input(preprocess_initial(train_files))

test_set_1 = preprocess_input(preprocess_initial(test_files))

valid_set_1 = preprocess_input(preprocess_initial(valid_files))

layers = ['new_pool_1','new_batch_1','new_dense_1','new_batch_2','new_drop_2','new_dene_2']
model_1.get_layer('new_dene_2').set_weights([np.array(weights[10],dtype=np.float32),np.zeros([3],dtype=np.float32)])
model_1.get_layer('new_dense_1').set_weights([np.array(weights[4],dtype=np.float32),np.zeros([512],dtype=np.float32)])

# the first 249 layers and unfreeze the rest:
for layer in model_1.layers[:249]:
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')
示例#12
0
def main():
    # Parameters
    if len(sys.argv) == 3:
        superclass = sys.argv[1]
        model_weight = sys.argv[2]
    else:
        print('Parameters error')
        exit()

    # The constants
    classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24}
    testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'}
    date = '20180321'

    # Feature extraction model
    base_model = Xception(include_top=True,
                          weights=None,
                          input_tensor=None,
                          input_shape=None,
                          pooling=None,
                          classes=classNum[superclass[0]])
    base_model.load_weights(model_weight)
    base_model.summary()
    model = Model(inputs=base_model.input,
                  outputs=base_model.get_layer('avg_pool').output)

    imgdir_train = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date\
                     +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date
    imgdir_test = '../zsl_' + testName[superclass[0]] + '_' + str(
        superclass).lower() + '_test_' + date
    categories = os.listdir(imgdir_train)
    categories.append('test')

    num = 0
    for eachclass in categories:
        if eachclass[0] == '.':
            continue
        if eachclass == 'test':
            classpath = imgdir_test
        else:
            classpath = imgdir_train + '/' + eachclass
        num += len(os.listdir(classpath))

    print('Total image number = ' + str(num))

    features_all = np.ndarray((num, 2048))
    labels_all = list()
    images_all = list()
    idx = 0

    # Feature extraction
    for iter in tqdm(range(len(categories))):
        eachclass = categories[iter]
        if eachclass[0] == '.':
            continue
        if eachclass == 'test':
            classpath = imgdir_test
        else:
            classpath = imgdir_train + '/' + eachclass
        imgs = os.listdir(classpath)

        for eachimg in imgs:
            if eachimg[0] == '.':
                continue

            img_path = classpath + '/' + eachimg
            img = image.load_img(img_path, target_size=(229, 299))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            feature = model.predict(x)

            features_all[idx, :] = feature
            labels_all.append(eachclass)
            images_all.append(eachimg)
            idx += 1

    features_all = features_all[:idx, :]
    labels_all = labels_all[:idx]
    images_all = images_all[:idx]
    data_all = {
        'features_all': features_all,
        'labels_all': labels_all,
        'images_all': images_all
    }

    # Save features
    savename = 'features_' + superclass + '.pickle'
    fsave = open(savename, 'wb')
    pickle.dump(data_all, fsave)
    fsave.close()
示例#13
0
# from keras.applications.resnet50 import ResNet50
from keras.applications.xception import Xception
from keras.preprocessing import image
from keras.applications.xception import preprocess_input, decode_predictions
from keras.utils.vis_utils import plot_model
import numpy as np
# import matplotlib.pyplot as plt

# model = ResNet50(weights='imagenet')
model = Xception()
print(model.summary())
# plot_model(model, to_file='xception.png')
model = Xception(weights='imagenet')
img_path = 'elephant.jpg'
# img_path = 'parrot.jpg'
img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
# plt.imshow(x / 255.)
# plt.show()
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
# 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=8)[0])
# img.show()
for pred in decode_predictions(preds, top=8)[0]:
    print(pred)
示例#14
0
def main():
    # Parameters
    if len(sys.argv) == 4:
        superclass = sys.argv[1]
        imgmove = sys.argv[2]
        if imgmove == 'False':
            imgmove = False
        else:
            imgmove = True
        lr = float(sys.argv[3])
    else:
        print('Parameters error')
        exit()

    # The constants
    classNum = {'A': 40, 'F': 40, 'V': 40, 'E': 40, 'H': 24}
    testName = {'A': 'a', 'F': 'a', 'V': 'b', 'E': 'b', 'H': 'b'}
    date = '20180321'

    trainpath = 'trainval_' + superclass + '/train'
    valpath = 'trainval_' + superclass + '/val'

    if not os.path.exists('model'):
        os.mkdir('model')

    # Train/validation data preparation
    if imgmove:
        os.mkdir('trainval_' + superclass)
        os.mkdir(trainpath)
        os.mkdir(valpath)
        sourcepath = '../zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_'+date+'_crop'\
                     +'/zsl_'+testName[superclass[0]]+'_'+str(superclass).lower()+'_train_images_'+date
        categories = os.listdir(sourcepath)
        for eachclass in categories:
            if eachclass[0] == superclass[0]:
                print(eachclass)
                os.mkdir(trainpath + '/' + eachclass)
                os.mkdir(valpath + '/' + eachclass)
                imgs = os.listdir(sourcepath + '/' + eachclass)
                idx = 0
                for im in imgs:
                    if idx % 8 == 0:
                        shutil.copyfile(
                            sourcepath + '/' + eachclass + '/' + im,
                            valpath + '/' + eachclass + '/' + im)
                    else:
                        shutil.copyfile(
                            sourcepath + '/' + eachclass + '/' + im,
                            trainpath + '/' + eachclass + '/' + im)
                    idx += 1

    # Train and validation ImageDataGenerator
    batchsize = 32

    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       rotation_range=15,
                                       width_shift_range=5,
                                       height_shift_range=5,
                                       horizontal_flip=True)

    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(trainpath,
                                                        target_size=(72, 72),
                                                        batch_size=batchsize)

    valid_generator = test_datagen.flow_from_directory(valpath,
                                                       target_size=(72, 72),
                                                       batch_size=batchsize)

    # Train MobileNet
    model = Xception(include_top=True,
                     weights=None,
                     input_tensor=None,
                     input_shape=(72, 72, 3),
                     pooling=None,
                     classes=classNum[superclass[0]])
    model.summary()
    model.compile(optimizer=SGD(lr=lr, momentum=0.9),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    steps_per_epoch = int(train_generator.n / batchsize)
    validation_steps = int(valid_generator.n / batchsize)

    weightname = 'model/mobile_' + superclass + '_wgt.h5'

    if os.path.exists(weightname):
        model.load_weights(weightname)

    checkpointer = ModelCheckpoint(weightname,
                                   monitor='val_loss',
                                   verbose=0,
                                   save_best_only=True,
                                   save_weights_only=True,
                                   mode='auto',
                                   period=1)
    model.fit_generator(train_generator,
                        steps_per_epoch=steps_per_epoch,
                        epochs=100,
                        validation_data=valid_generator,
                        validation_steps=validation_steps,
                        callbacks=[checkpointer])
示例#15
0
# In[ ]:

from keras.applications.xception import Xception

xception_weights_path = "xception_weights_tf_dim_ordering_tf_kernels_notop.h5"
xception_conv_base = Xception(include_top=False,
                              weights=None,
                              input_tensor=None,
                              input_shape=(224, 224, 3),
                              pooling=None,
                              classes=None)
xception_conv_base.load_weights(xception_weights_path)

#xception_conv_final_predictor = Xception(include_top=False, weights="imagenet", input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None)

xception_conv_base.summary()

#xception_conv_base.load_weights(xception_weights_path)

# In[ ]:

xception_conv_base.weights

# In[ ]:

from keras.models import Sequential, Model
from keras.layers import Flatten, Dense, Lambda, Input, Multiply, Dot, Add, Concatenate, Average
from keras import backend as K
import tensorflow as tf

示例#16
0
                 input_shape=(img_h, img_w, 3),
                 pooling=None,
                 classes=num_classes)

#x = Flatten()(base_model.output)
#predictions = Dense(num_classes, activation = 'softmax')(x)

#create graph of your new model
#model = Model(input = base_model.input, output = predictions)

#model = multi_gpu_model(model, gpus=2)
model.compile(loss='categorical_crossentropy',
              optimizer='Adam',
              metrics=['accuracy'])

print model.summary()

model_name = os.path.join(args.model_dir,
                          "doc_detect.{epoch:02d}-{val_loss:.2f}.hdf5")
checkpointer = ModelCheckpoint(model_name,
                               monitor='val_acc',
                               verbose=1,
                               save_best_only=True,
                               save_weights_only=False,
                               mode='auto',
                               period=1)

train_datagen = ImageDataGenerator(rescale=1. / 255)

test_datagen = ImageDataGenerator(rescale=1. / 255)
plt.plot(history5.history['loss'])
plt.plot(history5.history['val_loss'])
plt.title('Resnet152V2 model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.savefig('resnet152_loss')
plt.show()

"""## Xception Net"""

from keras.applications.xception import Xception

xcept = Xception(include_top=False, weights='imagenet')

xcept.summary()

x = xcept.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.3)(x)
predictions = Dense(4, activation='softmax')(x)

model = Model(inputs=xcept.input, outputs=predictions)

for layer in xcept.layers:
    layer.trainable = False

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy',f1_m,precision_m, recall_m])
示例#18
0
def main(args):

    # hyper parameters
    batch_size = 16
    num_classes = 102
    epochs = 100

    # Instantiate model
    model = Xception(include_top=True, weights=None, classes=num_classes)

    # prepare data
    x_train = np.load(os.path.join(current_directory, 'x_train.npy'))
    y_train = np.load(os.path.join(current_directory, 'y_train.npy'))
    x_test = np.load(os.path.join(current_directory, 'x_test.npy'))
    y_test = np.load(os.path.join(current_directory, 'y_test.npy'))

    # summary of the model
    model.summary()

    # compile model
    model.compile(
        loss=categorical_crossentropy,
        optimizer=Adadelta(),
        metrics=['accuracy']
    )

    # learning section
    hist = model.fit(
        x_train,
        y_train,
        batch_size=batch_size,
        epochs=epochs,
        verbose=1,
        validation_data=(x_test, y_test)
    )

    # evaluation section
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

    # save graphs
    acc = hist.history['acc']
    val_acc = hist.history['val_acc']
    loss = hist.history['loss']
    val_loss = hist.history['val_loss']

    plt.plot(range(epochs), loss, marker='.', label='acc')
    plt.plot(range(epochs), val_loss, marker='.', label='val_acc')
    plt.legend(loc='best')
    plt.grid()
    plt.xlabel('epoch')
    plt.ylabel('acc')
    plt.savefig(os.path.join(current_directory, 'acc_xception.png'))
    plt.clf()

    plt.plot(range(epochs), acc, marker='.', label='loss')
    plt.plot(range(epochs), val_acc, marker='.', label='val_loss')
    plt.legend(loc='best')
    plt.grid()
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.savefig(os.path.join(current_directory, 'loss_xception.png'))
    plt.clf()
示例#19
0
    pre_model = VGG19(weights='imagenet', include_top=False)
    detect_dog_model = VGG19(weights='imagenet')
elif settings.PRETRAINED_MODEL == 'InceptionV3':
    from keras.applications.inception_v3 import InceptionV3, preprocess_input
    pre_model = InceptionV3(weights='imagenet', include_top=False)
    detect_dog_model = InceptionV3(weights='imagenet')
elif settings.PRETRAINED_MODEL == 'Resnet50':
    from keras.applications.resnet50 import ResNet50, preprocess_input
    pre_model = ResNet50(weights='imagenet', include_top=False)
    detect_dog_model = ResNet50(weights='imagenet')
elif settings.PRETRAINED_MODEL == 'Xception':
    from keras.applications.xception import Xception, preprocess_input
    pre_model = Xception(weights='imagenet', include_top=False)
    detect_dog_model = Xception(weights='imagenet')

pre_model.summary()
model = load_model(settings.MODEL_TOP_LAYER)
model.summary()

graph = tf.get_default_graph()


def predict_img(img_path):

    with graph.as_default():

        tensor = path_to_tensor(img_path)
        pre_processed_img = preprocess_input(tensor)
        bottleneck_feature = pre_model.predict(tensor)
        predicted_vector = model.predict(bottleneck_feature)
        return settings.DOG_NAMES[np.argmax(predicted_vector)]
示例#20
0
testTulipssDir = os.path.join(testDir, 'Tulips')
print("Testing Folders Completed")
print("")
#######################################################
#######################################################
print("")  #build NN
#######################################################
#######################################################
print("Building Neural Network")
convBase = Xception(include_top=True,
                    weights='imagenet',
                    input_tensor=None,
                    input_shape=None,
                    pooling=None,
                    classes=1000)
convBase.summary()
print("Completed Importing InceptionV3")

print("Scale Images Generator")
dataGen = ImageDataGenerator(rescale=1. / 255)
batchSize = 20
print("Completed Scale Images Generator")


def extractFeatures(directory, sampleCount):
    features = np.zeros(shape=(sampleCount, 4, 4, 512))
    labels = np.zeros(shape=(sampleCount))
    generator = dataGen.flow_from_directory(directory,
                                            target_size=(150, 150),
                                            batch_size=batchSize,
                                            class_mode='binary')
# Load the dataset
test_dir = "test/"

# data generator sem o augmentation - para a validação
datagen_no_aug = ImageDataGenerator(rescale=1. / 255)

# Create the model
input_img = Input(shape=(299, 299, 3))

# pre-trained model
pt_model = Xception(include_top=False,
                    weights='imagenet',
                    input_tensor=input_img,
                    input_shape=(299, 299, 3),
                    pooling='avg')
print pt_model.summary()

for layer in pt_model.layers[:-15]:
    layer.trainable = False

for layer in pt_model.layers[-15:]:
    layer.trainable = True

# new fully connected layer
x = pt_model.layers[-1].output
"""
conv1 = Conv2D(32, kernel_size=(3,3), padding='valid')(x)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
conv1 = Conv2D(32, kernel_size=(3,3), padding='valid')(conv1)
conv1 = BatchNormalization()(conv1)
示例#22
0
sortd.tail()

# In[ ]:
#Class weighting
cw = np.median(sortd.values) / sortd.values
print(cw)

# In[ ]:

from keras.applications.xception import Xception

premodel = Xception(include_top=False,
                    weights='imagenet',
                    input_shape=(299, 299, 3),
                    classes=num_classes)
premodel.summary()

# In[ ]:

# for layer in premodel.layers[:5]:
#     layer.trainable = False

# In[ ]:

from keras.layers.pooling import GlobalAveragePooling2D
from keras.layers import Dense

#Adding custom Layers
# x = model.output
# x = Flatten()(x)
# x = Dense(1024, activation="relu")(x)
示例#23
0
    if IMG_SIZE !=224:
        model = MobileNetV2(weights='imagenet',include_top=False,input_shape=(224, 224,3))
        new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3))
    else:
        new_model = MobileNetV2(weights='imagenet',include_top=False,input_shape=(224, 224,3))
elif themodel =='ResNet50':
    from keras.applications import ResNet50
    if IMG_SIZE !=224:
        model = ResNet50(weights='imagenet',include_top=False,input_shape=(224, 224,3))
        new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3))
    else:
        new_model = ResNet50(weights='imagenet',include_top=False,input_shape=(224, 224,3))
    


new_model.summary()


# In[26]:


def build_model():
    model = Sequential()
    model.add(new_model)
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(13, activation='sigmoid'))
    model.add(layers.Dense(1, activation='sigmoid'))
    
    model.compile(
        loss='binary_crossentropy',
示例#24
0
print('1')
import os
from pathlib import Path
from os import listdir
import pandas as pd
import numpy as np
import sklearn.model_selection as m
import tensorflow as tf
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.xception import preprocess_input,Xception
from keras.models import Model

#%%
x=Xception()
x.summary()
#%%
def extract_features(directory):
    dir1=directory
    #extracting features
    model=Xception()
    #removing the last output layer
    model.layers.pop()
    #defining the Model which will take the input the size of the imahe and output the values in the last layer
    model=Model(inputs=model.inputs,output=model.layers[-1].output)
    
    print(model.summary())
    features=dict()
    file=os.listdir(dir1)
    for name in file:
        path=os.path.join(dir1,name)
示例#25
0
x = layers.MaxPool2D(pool_size=(3, 3), strides=2, padding='same')(x)
x = layers.add([x, res])

# layer 20 #
x = SeparableConv2D(filters=1536,
                    kernel_size=(3, 3),
                    strides=1,
                    padding='same',
                    use_bias=False)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

# layer 21 #
x = SeparableConv2D(filters=2048,
                    kernel_size=(3, 3),
                    strides=1,
                    padding='same',
                    use_bias=False)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = layers.GlobalAveragePooling2D()(x)
output = layers.Dense(units=1000, activation='softmax')(x)

xception = models.Model(input_layer, output)
xception.summary()

### examples ###
xc = Xception(input_shape=(299, 299, 3))
xc.summary()
示例#26
0
fit_epochs = 50
batch_size = 24
nb_classes = 5
nb_epoch = 10

#build CNN

model_Xception_conv = Xception(weights='imagenet', include_top=False)

input = Input(shape=(img_width, img_height, 3),name = 'image_input')

output_vgg16_conv = model_Xception_conv(input)

for layer in model_Xception_conv.layers[:15]:
    layer.trainable = False
model_Xception_conv.summary()

x = Flatten(name='flatten')(output_vgg16_conv)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(5, activation='softmax', name='predictis')(x)

xception_model = Model(inputs=input, outputs=x)

xception_model.summary()


#Image preprocessing and image augmentation with keras
xception_model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.SGD(lr=1e-3, momentum=0.9),
              metrics=['accuracy']
示例#27
0
elif args.model == "NASNetLarge":
    from keras.applications.nasnet import preprocess_input
    from keras.applications.nasnet import decode_predictions
    preprocessing_function = preprocess_input
    base_model = NASNetLarge()
    image = load_img('soccer_ball.jpeg', target_size=(224, 224))
elif args.model == "NASNetMobile":
    from keras.applications.nasnet import preprocess_input
    from keras.applications.nasnet import decode_predictions
    preprocessing_function = preprocess_input
    base_model = NASNetMobile()
    image = load_img('soccer_ball.jpeg', target_size=(224, 224))
else:
    ValueError("The model you requested is not supported in Keras")

base_model.summary()

# 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 = base_model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2] * 100))

if __name__ == "__main__":
    train_dir = 'data/train'
    valid_dir = 'data/valid'
    test_dir = 'data/test'

    # load file paths
    train_paths = glob.glob(os.path.join(train_dir, '*', '*.jpg'))
    valid_paths = glob.glob(os.path.join(valid_dir, '*', '*.jpg'))
    test_paths = glob.glob(os.path.join(test_dir, 'unknown', '*.jpg'))
    random.shuffle(train_paths)

    # load pretrained model
    xception_bottleneck = Xception(weights='imagenet', include_top=False, pooling='avg')
    xception_bottleneck.summary()

    # extract Xception bottleneck features
    if not os.path.exists('train_feats_xception.npy'):
        train_feats = extract_features(xception_bottleneck, train_paths)
        valid_feats = extract_features(xception_bottleneck, valid_paths)
        test_feats = extract_features(xception_bottleneck, test_paths)

        # save features
        np.save('train_feats_xception.npy', train_feats)
        np.save('valid_feats_xception.npy', valid_feats)
        np.save('test_feats_xception.npy', test_feats)
    else:
        train_feats = np.load('train_feats_xception.npy')
        valid_feats = np.load('valid_feats_xception.npy')
        test_feats = np.load('test_feats_xception.npy')
示例#29
0
def Make_Model(modelConfig, datasetConfig):

    strModelType = modelConfig.MODEL_TYPE
    strPretrained = modelConfig.PRETRAINED_MODEL
    im_Shape = datasetConfig.IMG_SHAPE
    strOptimizer = modelConfig.OPTIMIZER
    num_Classes = datasetConfig.NUM_CLASS
    learingRate = modelConfig.LEARNING_RATE
    decay = modelConfig.DECAY
    momentum = modelConfig.MOMENTUM
    loss = modelConfig.LOSS

    optimizer = None

    if (strOptimizer == "SGD"):
        optimizer = SGD(lr=learingRate,
                        decay=decay,
                        momentum=momentum,
                        nesterov=True)  # decay = 1e-4
    elif (strOptimizer == "ADAM"):
        optimizer = Adam(lr=learingRate, decay=decay)
    else:
        print("No Such a Optimizer")
        return None

    model = None

    Num = 2

    if (strModelType == "VGG16"):
        model = VGG16(weights=strPretrained,
                      include_top=False,
                      input_shape=im_Shape,
                      classes=Num)
    elif (strModelType == "RESNET50"):
        model = ResNet50(weights=strPretrained,
                         include_top=True,
                         input_shape=im_Shape,
                         classes=Num)
    elif (strModelType == "RESNET152"):
        model = build_Resnet152_Model(im_Shape, Num, strPretrained)
    elif (strModelType == "INCEPTIONV3"):
        model = InceptionV3(weights=strPretrained,
                            include_top=True,
                            input_shape=im_Shape,
                            classes=Num)
    elif (strModelType == "INCEPTIONRESV2"):
        model = InceptionResNetV2(weights=strPretrained,
                                  include_top=True,
                                  input_shape=im_Shape,
                                  classes=Num)
    elif (strModelType == "SEINCEPTIONRESV2"):
        model = SEInceptionResNetV2(weights=strPretrained,
                                    include_top=True,
                                    input_shape=im_Shape,
                                    classes=Num)
    elif (strModelType == "XCEPTION"):
        model = Xception(weights=strPretrained,
                         include_top=True,
                         input_shape=im_Shape,
                         classes=Num)
        #basemodel = Xception(weights="imagenet", include_top=True, input_shape=(299,299,3), classes = 1000)
        #x = Dense(num_Classes, activation='softmax', name='predictions')(basemodel.layers[-2].output)
        #model = Model(basemodel.input, x)
    elif (strModelType == "UNET2D"):
        model = build_UNet2D_4L(im_Shape, strPretrained)
    elif (strModelType == "CNN6Layers"):
        model = build_CNN_6layers(im_Shape, num_classes=num_Classes)
    else:
        print("No Such Model Type")
        return None

    # for layer in model.layers[:-15] :
    #     layer.trainable = False

    # oldlayer = model.layers.pop(-1)
    # model.layers.pop(-1)
    x_xc = model.get_layer(index=-2).output
    #print(x_xc.output_shape)
    # print(x_xc.shape)
    #x_xc = GlobalMaxPooling2D()(x_xc)
    out = Dense(int(num_Classes), activation='softmax', name='pp')(x_xc)

    model = Model(input=model.input, output=out)

    # upperlayer = model.layers[-2]
    # output = Dense(int(num_Classes), activation='softmax')(upperlayer)
    #model.trainable = False
    #model = model (input = model.layers[0], output = output)
    model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
    model.summary()

    return model
示例#30
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