total_classes = res['total_classes'] Id2Vec = np.zeros([len(Id2Word.keys()), embedding_size]) words_list = word_vecs.word2vec.keys() for i in range(len(Id2Word.keys())): word = Id2Word[i] if word in words_list: Id2Vec[i, :] = word_vecs.word2vec[word] else: Id2Vec[i, :] = word_vecs.word2vec['unknown'] m = model(max_sequence_length=max_sequence_length, total_classes=1, embedding_size=300, char_size=len(char2Id.keys()), char_embed_size=9, id2Vecs=Id2Vec, batch_size=60, max_word_len=max_word_len, threshold=0.5) ## split data to train and test n = len(data_1) q = 0.05 # ratio of test and train test_data_len = int(q * n) a = random.sample(range(1, n), int(q * n)) ## ids for test data test_data_1 = np.zeros([test_data_len, max_sequence_length]) test_data_chars_1 = np.zeros( [test_data_len, max_sequence_length, max_word_len]) test_data_2 = np.zeros([test_data_len, max_sequence_length]) test_data_chars_2 = np.zeros(
test_set_x = test_set_x_orig.reshape(m, -1).T test_set_y = test_set_y_orig.reshape(1, m) return test_set_x / 255, test_set_y test_set_x, test_set_y = read_test_data() train_set_x, train_set_y = read_train_data() learning_rates = [0.001, 0.0001] results = {} for i in learning_rates: print('learning_rate:' + str(i)) results[str(i)] = simple_model.model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=3000, learning_rate=i) for i in learning_rates: plt.plot(np.squeeze(results[str(i)]["costs"]), label=str(i)) plt.ylabel('cost') plt.xlabel('iterations') legend = plt.legend(loc='upper center', shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90') plt.show() dir_path = os.path.dirname(__file__)
def CNN_main(train_data,test_data,result_path,train_labels,test_labels,num_classes,epoch,batch_size_factor,optimizer,CNN_model,train_CNN,feature_extraction,feature_extractor_parameters): ''' Function Description: This function is the main function for the CNN modules and it controls them all, it decides which method to be used , CNN as a classifer or as a feature extractor and for each one of them which CNN model and architetcture to be used and also the paramteres for each one of the case is defined by the variables as described later. Function Parameters: ------------------------------------------------------- Train_data_file_name:The name of the train data file , this will be differ depeneding on the CNN model used,For the following models 'ResNet50','inception_model,'DenseNet121','VGG_pretrained' a resized data to diminssion 224*224*3 will be used , this one was resized from the original data and was saved after, as this conversion takes alot of time so as to decrease the computation time. Test_file_name:The name of the test data file and there are manily two files as mentioned in the previous variable. results_directory:the directory name where you would like the output files to be generated train_data_file_name:This is used to load the training data labels , also you should choose the right file as there are different training data files due to differnet augmentation methods used and they differ in their number so the lables will differ also test_data_file_name: the name of the test labels file , this will also be the same as long as the test data is not changable. num_classes:the number of the classes the our data will be classifed to. epoch:the number of iter through all the training dataset that the CNN model will go through batch_size_factor:this will take the batch as a factor of the training data size and since the trainig data in our case is small , then it will be alawys =1 optimizer:this will be the optimizer used for training the CNN model, the variable should have the name of one of the optimizer in the code , else error will occur. CNN_model:this define which CNN model to be trained on the training dataset or it will be None if a saved model will be used as feature extractor train_CNN: This selects one of the two modes ,=0 means to train a CNN_model or =1 to use a pretrained model or a saved model to extrat features from the training data directly. feature_extraction:This is variable wil be used only if train_CNN =0 and it will select between two options; =0 to trian the CNN model only without using it for feature extraction and if it is =1 the model after being trained it will be saved and used for feature extraction. feature_extractor_parameters: This is a dict of four variables and they are used as an input parameters for the feature extraction functionan they are as the following : 'CNN_model': this will be the CNN model used for feature extraction, it may takes different type , it may be string , in the case of pretrained model so the string will be equal to the name of the selected model or it may take 'all' in this case all the pretrained models will be used , in case of saved model this can take two forms, the first to be string if the feature_extraction=1 , this mean that a saved model will be used directly without being trained and if feature_extraction=1 it will be th model that have just been trained and saved. 'model_type':to choose between the pretrained models on imagenet dataset or the saved trained models on the training set. 'classifer_name': this is the classifer name you would like to use , it can be 'all', or one of them. 'hyperprametres': this is another dict in which the classifer hyperparmaters is defined, it contain four main parameters , the value of the neigbors to look to for the KNN classifer and the for SVC classifer thera are two hyperparamters 'penalty' and 'C' and for the fully conncted classifer there are four hyperparamters 'dropouts','activation_functions','opt','epoch'.If any of this classifers is not used then it's hyperparmater should be =None. ''' opt=optimizers.choosing(optimizer) if train_CNN==1: if CNN_model=='simple_model': simple_model.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='LeNet': LeNet.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='AlexNet': AlexNet.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='ZFNet': ZFNet.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='VGG': VGG.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='VGG_pretrained': VGG_pretrained.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='ResNet50': ResNet50.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model=='inception_model': InceptionResNetV2.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) elif CNN_model== 'DenseNet121': DenseNet121.model(train_data,train_labels,test_data,test_labels,opt,epoch,batch_size_factor,num_classes,result_path,feature_extraction,feature_extractor_parameters) else: print('Value Error: CNN_model took unexpected value') sys.exit() elif train_CNN==0: CNN_feature_extractor.CNN_feature_extraction_classsification(feature_extractor_parameters,result_path) else: print('Value Error: train_CNN took unexpected value') sys.exit()
data_augmentation = True num_predictions = 20 save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'keras_cifar10_trained_model.h5' # The data, shuffled and split between train and test sets: (x_train, y_train), (x_test, y_test) = cifar100.load_data() print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # Convert class vectors to binary class matrices. y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = simple_model.model(x_train.shape[1:], num_classes) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 if not data_augmentation: print('Not using data augmentation.') model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True) else:
import keras.backend as K import io_functions import simple_model import pandas as pd import matplotlib.pyplot as plt #----------------------------------------------------------------------- # Hyper-parameters num_train = 10 # while protoyping, otherwise: len(ids_train) learning_rate = 1e-3 num_epochs = 2 batch_size = 2 #----------------------------------------------------------------------- # load model architecture model = simple_model.model() #----------------------------------------------------------------------- from keras.optimizers import Adam from keras.losses import binary_crossentropy smooth = 1. # From here: https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)