-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage rec.py
93 lines (54 loc) · 2.03 KB
/
image rec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Tensorflow and numpy to create the neural network
import tensorflow as tf
import numpy as np
# Matplotlib to plot info to show our results
import matplotlib.pyplot as plt
# OS to load files and save checkpoints
import os
%matplotlib inline
# Load MNIST data from tf examples
image_height = 28
image_width = 28
color_channels = 1
model_name = "mnist"
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
category_names = list(map(str, range(10)))
# TODO: Process mnist data
# Load cifar data from file
image_height = 32
image_width = 32
color_channels = 3
model_name = "cifar"
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
cifar_path = './cifar-10-data/'
train_data = np.array([])
train_labels = np.array([])
# Load all the data batches.
for i in range(1, 6):
data_batch = unpickle(cifar_path + 'data_batch_' + str(i))
train_data = np.append(train_data, data_batch[b'data'])
train_labels = np.append(train_labels, data_batch[b'labels'])
# Load the eval batch.
eval_batch = unpickle(cifar_path + 'test_batch')
eval_data = eval_batch[b'data']
eval_labels = eval_batch[b'labels']
# Load the english category names.
category_names_bytes = unpickle(cifar_path + 'batches.meta')[b'label_names']
category_names = list(map(lambda x: x.decode("utf-8"), category_names_bytes))
# TODO: Process Cifar data
def process_data(data):
float_data = np.array(data, dtype=float) / 255.0
reshaped_data = np.reshape(float_data, (-1, color_channels, image_height, image_width))
# The incorrect image
transposed_data = np.transpose(reshaped_data, [0, 2, 3, 1])
return transposed_data
train_data = process_data(train_data)
eval_data = process_data(eval_data)