Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.2 kB
11
Indexable
Never
def get_UTK(options  = {'bs':512, 'label': 'gender'}):
    torch.manual_seed(0)
    label = options['label']
    pd00 = pd.read_csv(utk_data_path, compression='gzip')
    age_bins = [0, 10, 15, 20, 25, 30, 40, 50, 60, 120]
    age_labels = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    pd00['age_bins'] = pd.cut(pd00.age, bins=age_bins, labels=age_labels)
    X = pd00.pixels.apply(lambda x: np.array(x.split(" "), dtype=float))
    X = np.stack(X)
    X = X / 255.0
    X = X.astype('float32').reshape(X.shape[0], 1, 48, 48)
    y = pd00[label].values
    np.random.seed(0)  # random seed of partition data into train/test
    x_train, x_test, y_train, y_test  = train_test_split(X, y,  test_size=0.2)
    train_tensor = TensorDataset(torch.FloatTensor(x_train), torch.LongTensor(y_train))
    train_loader = DataLoader(dataset=train_tensor, batch_size= options['bs'], shuffle=True)
    x_test, y_test = torch.FloatTensor(x_test).cuda(), torch.LongTensor(y_test).cuda()
    n_class =  int ( torch.max(y_test).item() + 1)
    params = {'x_test':x_test, 'y_test':y_test, 'train_loader':train_loader,
        'n_channel':1, 'n_hidden' :576, 'n_all':  1024, 'n_out':n_class, 'n_hidden_google':25600}

    params['n_in'] = np.prod(x_test.shape[1:])

    return params