criterion = nn.CrossEntropyLoss(size_average=True)
    optimizer_ft = optim.SGD(list(model_ft.parameters())[-32:],
                             lr=0.01,
                             momentum=0.9)
    #  optimizer_ft = optim.Adam(model_ft.parameters(), lr=0.001)

    train_model(model_ft,
                criterion,
                optimizer_ft,
                exp_lr_scheduler,
                num_epochs=n_epoch,
                kid=kid)


for i in range(kfold):
    train_val_split(kfold, i)

    train_val_folder = {
        x: datasets.ImageFolder(os.path.join(DIR_BASE, x), data_transforms[x])
        for x in ['train', 'val']
    }
    train_val_loader = {
        x: torch.utils.data.DataLoader(train_val_folder[x],
                                       batch_size=BATCH_SIZE,
                                       shuffle=True,
                                       num_workers=12)
        for x in ['train', 'val']
    }
    train_val_size = {x: len(train_val_folder[x]) for x in ['train', 'val']}
    classes = train_val_folder['train'].classes
    # print('There are {} classes, {} images in train set and {} images in validation set.'
import os
from scipy import ndimage, misc
import scipy.misc
import re, glob
import cv2
from train_val_split import train_val_split

os.environ["CUDA_VISIBLE_DEVICES"] = '0'

root_train = ''
root_val = ''
root_total = ''

#classes = ['',''.....]

train_val_split(prop=0.8)

train_imgs = glob.glob('')

for filename in train_imgs:
    image = ndimage.imread(filename, mode="RGB")
    image_resized = misc.imresize(image, (299, 299))
    out_file = filename.split('.')[0].split('/')[1]
    cv2.imwrite(filename, image_resized)

learning_rate = 0.0001
img_width = 299
img_height = 299
nbr_train_samples = ''
nbr_validation_samples = ''
nbr_epochs = 50
예제 #3
0
def eval_CNN_regression(experiment_name, trials, inFile, image_path, categoryName, gpus, model_type, epoch_number, augment_number, learning_rate, decay, regularization, batch_size, img_height, img_width):
    
    #Experiment folder
    try:
        os.mkdir(os.path.join(main_experiment_path, experiment_name))
    except:
        print('Saving over experiment ' + experiment_name)
        
    means = list()
    variances = list()
    
    for i in range(1, trials+1):
        
        trial_path = os.path.join(main_experiment_path, experiment_name, experiment_name+'_'+str(i))
        
        #Trial i folder
        try:
            os.mkdir(trial_path)
        except:
            print('Saving over trial ' + str(i) + ' of experiment ' + experiment_name)
           
        df = pd.read_csv(inFile)
        split_objects(df, os.path.join(trial_path, 'splitdf.txt'))

        y_classes = train_val_split(os.path.join(trial_path, 'splitdf.txt'), outFile=os.path.join(trial_path, 'df'), categoryName=categoryName, numCat=0, val_prop=0.3)
        
        train_path = os.path.join(trial_path, 'df_train.txt')
        val_path = os.path.join(trial_path, 'df_val.txt')
        
        #Train model i
        train_regression(gpus=gpus,
                         experiment_path=trial_path,
                         train_df_path=train_path,
                         train_image_path=image_path,
                         val_df_path=val_path,
                         val_image_path=image_path,
                         y_name=categoryName,
                         augment_number=augment_number,
                         model_type=model_type,
                         model_save_name=experiment_name,
                         epoch_number=epoch_number,
                         learning_rate=learning_rate,
                         decay=decay,
                         regularization=regularization,
                         batch_size=batch_size,
                         img_height=img_height,
                         img_width=img_width
                         )
        
        #Evaluate model i
        i_stats = eval_regression(experiment_path=trial_path,
                                  test_df_path=val_path,
                                  test_image_path=image_path,
                                  y_name=categoryName,
                                  model_test_name=experiment_name,
                                  height=img_height,
                                  width=img_width
                                  )
        
        #Top 1 accuracy
        means.append(i_stats['mean'])
        variances.append(i_stats['variance'])

    #Mean and variance of means and variances
    meanofmeans = statistics.mean(means)
    varianceofmeans = statistics.variance(means)
    meanofvariances = statistics.mean(variances)
    varianceofvariances = statistics.variance(variances)

    #Write summary of test results
    with open(os.path.join(main_experiment_path, experiment_name, experiment_name+':experiment_summary.txt'), 'w') as summary_writefile:
        summary_writefile.write('Regressing on ' + categoryName + '\n')
        summary_writefile.write('Mean error in years: ' + str(meanofmeans) + '\n')
        summary_writefile.write('Mean error variance in years: ' + str(meanofvariances) + '\n')
        summary_writefile.write('Variance of mean error in years: ' + str(varianceofmeans) + '\n')
        summary_writefile.write('Variance of variances in years: ' + str(varianceofvariances) + '\n')
        for i in range(1, len(means)+1):
            summary_writefile.write('Model ' + str(i) + ': ' + 'mean: ' + str(means[i-1]) + ', v ariance: ' + str(variances[i-1]) + '\n')
        
    #Return test results summary
    return {'meanofmeans': meanofmeans,
            'varianceofmeans': varianceofmeans,
            'meansofvariances': meanofvariances,
            'varianceofvariances': varianceofvariances}
예제 #4
0
params = {"batch_size": batch_sz, "shuffle": False, "num_workers": 4}

train_pct = 0.7
device = "cpu"

if __name__ == '__main__':
    # Extract data for the ticker mentioned above
    extractHistory(SYMBOL, start_date, end_date, pth)

    # Get the inputs and outputs from the extracted ticker data
    inputs, labels, dates = curateData(pth, "Close", "Date", n_lags)
    N = len(inputs)

    # Perform the train validation split
    trainX, trainY, valX, valY = train_val_split(inputs, labels, train_pct)

    # Standardize the data to bring the inputs on a uniform scale
    trnX, SS_ = standardizeData(trainX, train=True)
    valX, _ = standardizeData(valX, SS_)

    # Create dataloaders for both training and validation datasets
    training_generator = getDL(trnX, trainY, params)
    validation_generator = getDL(valX, valY, params)

    # Create the model
    model = forecasterModel(n_lags, hidden_dim, rnn_layers, dropout).to(device)

    # Define the loss function and the optimizer
    loss_func = nn.MSELoss()
    optim = torch.optim.Adam(model.parameters(), lr=learning_rate)
예제 #5
0
def eval_CNN_categorical(experiment_name, trials, inFile, image_path, categoryName, numCat, gpus, model_type, epoch_number, augment_number, learning_rate, decay, regularization, batch_size, img_height, img_width):
    
    #Experiment folder
    try:
        os.mkdir(os.path.join(main_experiment_path, experiment_name))
    except:
        print('Saving over experiment ' + experiment_name)
        
    top_1_accuracies = list()
    
    for i in range(1, trials+1):
        
        trial_path = os.path.join(main_experiment_path, experiment_name, experiment_name+'_'+str(i))
        
        #Trial i folder
        try:
            os.mkdir(trial_path)
        except:
            print('Saving over trial ' + str(i) + ' of experiment ' + experiment_name)
           
        df = pd.read_csv(inFile)
        split_objects(df, os.path.join(trial_path, 'splitdf.txt'))

        y_classes = train_val_split(os.path.join(trial_path, 'splitdf.txt'), outFile=os.path.join(trial_path, 'df'), categoryName=categoryName, numCat=numCat, val_prop=0.3)
        
        train_path = os.path.join(trial_path, 'df_train.txt')
        val_path = os.path.join(trial_path, 'df_val.txt')
        
        #Train model i
        train_categorical(gpus=gpus,
                          experiment_path=trial_path,
                          train_df_path=train_path,
                          train_image_path=image_path,
                          val_df_path=val_path,
                          val_image_path=image_path,
                          y_name=categoryName,
                          y_classes=y_classes,
                          augment_number=augment_number,
                          model_type=model_type,
                          model_save_name=experiment_name,
                          epoch_number=epoch_number,
                          learning_rate=learning_rate,
                          decay=decay,
                          regularization=regularization,
                          batch_size=batch_size,
                          img_height=img_height,
                          img_width=img_width
                          )
        
        #Evaluate model i
        k_accuracies = eval_categorical(experiment_path=trial_path,
                                        test_df_path=val_path,
                                        test_image_path=image_path,
                                        y_name=categoryName,
                                        model_test_name=experiment_name,
                                        height=img_height,
                                        width=img_width
                                        )
        
        #Top 1 accuracy
        top_1_accuracies.append(k_accuracies[0])
        
    #Mean and variance of top_1 accuracies
    mean = statistics.mean(top_1_accuracies)
    variance = statistics.variance(top_1_accuracies)
    
    #Write summary of test results
    with open(os.path.join(main_experiment_path, experiment_name, experiment_name+':experiment_summary.txt'), 'w') as summary_writefile:
        summary_writefile.write('Predicting ' + categoryName + ', trained on top ' + str(numCat) + ' classes\n')
        summary_writefile.write('Prediction error mean in years: ' + str(mean) + '\n')
        summary_writefile.write('Prediction error variance in years: ' + str(variance) + '\n')
        for i in range(1, len(top_1_accuracies)+1):
            summary_writefile.write('Accuracy of model ' + str(i) + ': ' + str(top_1_accuracies[i-1]) + '\n')
        
    #Return test results summary
    return {'mean': mean,
            'variance': variance}