Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sat Nov 16 16:24:23 2019
- @author: Tuguldur
- """
- import pickle
- def unpickle(file):
- fo = open(file, 'rb')
- dict = pickle.load(fo, encoding='latin1')
- fo.close()
- return dict
- import numpy as np
- def clean(data):
- imgs = data.reshape(data.shape[0], 3, 32, 32)
- grayscale_imgs = imgs.mean(1)
- cropped_imgs = grayscale_imgs[:, 4:28, 4:28]
- img_data = cropped_imgs.reshape(data.shape[0], -1)
- img_size = np.shape(img_data)[1]
- means = np.mean(img_data, axis=1)
- meansT = means.reshape(len(means), 1)
- stds = np.std(img_data, axis=1)
- stdsT = stds.reshape(len(stds), 1)
- adj_stds = np.maximum(stdsT, 1.0 / np.sqrt(img_size))
- normalized = (img_data - meansT) / adj_stds
- return normalized
- def read_data(directory):
- names = unpickle('{}/batches.meta'.format(directory))#['label_names']
- print('names', names)
- data, labels = [], []
- for i in range(1, 6):
- filename = '{}/data_batch_{}'.format(directory, i)
- batch_data = unpickle(filename)
- if i == 1:
- print('batch_data=', batch_data)
- if len(data) > 0:
- data = np.vstack((data, batch_data['data']))
- labels = np.hstack((labels, batch_data['labels']))
- else:
- data = batch_data['data']
- labels = batch_data['labels']
- print(np.shape(data), np.shape(labels))
- data = clean(data)
- data = data.astype(np.float32)
- return names, data, labels
- names, data, labels = read_data('E:/Machine learning/CNN/Cifar_data/cifar-10-python/cifar-10-batches-py')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement