Пример #1
0
    def fit(self, X_train, Y_train, X_val=None, y_val=None):
        Y_train = Y_train.astype('uint8')
        Y_train = np_utils.to_categorical(Y_train, self.nb_classes)
        y_val = np_utils.to_categorical(y_val, self.nb_classes)

        early_stop = MyEarlyStopping(patience=10, verbose=0)

        filename = "weights_%s_%s_%s.h5" % (self.target, self.mode,
                                            self.modelname)
        checkpointer = MyModelCheckpoint(filename,
                                         verbose=0,
                                         save_best_only=True)

        if (y_val is None):
            self.model.fit(X_train,
                           Y_train,
                           batch_size=self.batch_size,
                           epochs=self.epochs,
                           validation_split=0.2,
                           callbacks=[early_stop, checkpointer],
                           verbose=2)
        else:
            self.model.fit(X_train,
                           Y_train,
                           batch_size=self.batch_size,
                           epochs=self.epochs,
                           validation_data=(X_val, y_val),
                           callbacks=[early_stop, checkpointer],
                           verbose=2)
        self.model.load_weights(filename)
        if self.mode == 'cv':
            os.remove(filename)
        return self
Пример #2
0
	def fit(self,X_train,Y_train,X_val=None, y_val=None):
		Y_train = Y_train.astype('uint8')
		Y_train = np_utils.to_categorical(Y_train, self.nb_classes)
		y_val = np_utils.to_categorical(y_val, self.nb_classes)

		early_stop = MyEarlyStopping(patience=10, verbose=0)

		filename = "weights_%s_%s_%s.h5" %(self.target, self.mode, self.modelname)
		checkpointer = MyModelCheckpoint(filename,
            verbose=0, save_best_only=True)

		if (y_val is None):
			self.model.fit(X_train, Y_train, batch_size=self.batch_size,
                           epochs=self.epochs,validation_split=0.2,
                           callbacks=[early_stop,checkpointer], verbose=2
                           )
		else:
			self.model.fit(X_train, Y_train, batch_size=self.batch_size,
						   epochs=self.epochs,validation_data=(X_val,y_val),
						   callbacks=[early_stop,checkpointer], verbose=2
						   )
		self.model.load_weights(filename)
		# print out some weights to see if they are binary
		# print ('DEBUG: View weights.............')
		# for layer in self.model.layers:
		# 	config = layer.get_config()
		# 	if config['name'] == 'conv1':
		# 		print (layer.get_config())
		# 		print (layer.get_weights())
		if self.mode == 'cv':
			os.remove(filename)
		return self
Пример #3
0
    def fit_7(self, training_generator, validation_generator,
              class_weights_train):
        early_stop = MyEarlyStopping(patience=20, verbose=0)
        checkpointer = MyModelCheckpoint(filepath="2channel_sftf_7_train.h5",
                                         verbose=0,
                                         save_best_only=True)
        #class_weight = {0: 20.,
        #1: 1,
        #}
        self.model.fit_generator(generator=training_generator,
                                 validation_data=validation_generator,
                                 epochs=self.epochs,
                                 class_weight=class_weights_train,
                                 use_multiprocessing=False,
                                 workers=4,
                                 callbacks=[early_stop, checkpointer])

        #self.model.load_weights("weights_best.h5")
        # if self.mode == 'cv':
        # os.remove("weights_%s_%s.h5" %(self.target, self.mode))
        return self
    def fit(self,
            training_generator,
            validation_generator,
            class_weights,
            weightpath="weights_best.h5"):

        early_stop = MyEarlyStopping(patience=10, verbose=0)
        checkpointer = MyModelCheckpoint(filepath=weightpath,
                                         verbose=0,
                                         save_best_only=True)

        self.model.fit_generator(
            generator=training_generator,
            validation_data=validation_generator,
            epochs=self.epochs,
            class_weight=class_weights,
            use_multiprocessing=False,  #workers=2,
            callbacks=[early_stop, checkpointer])

        # self.model.load_weights(weightpath)
        #if self.mode == 'cv':
        #os.remove("weights_%s_%s.h5" %(self.target, self.mode))
        return self