Reputation: 1175
here is my thing:
Instead of running on colab, I want to read a local CIFAR10 dataset do play CNN using code from colab. Some first of all, I successfully downloaded the CIFAR10 dataset. Then I used the following code to read it:
import tensorflow as tf
import pandas as pd
import numpy as np
import math
import timeit
import matplotlib.pyplot as plt
from six.moves import cPickle as pickle
import os
import platform
from subprocess import check_output
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# %matplotlib inline
img_rows, img_cols = 32, 32
input_shape = (img_rows, img_cols, 3)
def load_pickle(f):
version = platform.python_version_tuple()
if version[0] == '2':
return pickle.load(f)
elif version[0] == '3':
return pickle.load(f, encoding='latin1')
raise ValueError("invalid python version: {}".format(version))
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
datadict = load_pickle(f)
X = datadict['data']
Y = datadict['labels']
X = X.reshape(10000,3072)
Y = np.array(Y)
return X, Y
def load_CIFAR10(ROOT):
""" load all of cifar """
xs = []
ys = []
for b in range(1,6):
f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
X, Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
del X, Y
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
return Xtr, Ytr, Xte, Yte
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
# Load the raw CIFAR-10 data
cifar10_dir = './cifar10/'
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
# Subsample the data
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
x_train = X_train.astype('float32')
x_test = X_test.astype('float32')
x_train /= 255.0
x_test /= 255.0
return x_train, y_train, X_val, y_val, x_test, y_test
# Invoke the above function to get our data.
x_train, y_train, x_val, y_val, x_test, y_test = get_CIFAR10_data()enter code here
Then, to display the images in the dataset, I used the original code from the link I mentioned:
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i], cmap=plt.cm.binary)
# The CIFAR labels happen to be arrays,
# which is why you need the extra index
plt.xlabel(classes[y_train[i][0]])
plt.show()
At last, unexpectedly, it gave an error saying:
runfile('F:/Google Drive/DCM_Image_AI/untitled1.py', wdir='F:/Google Drive/DCM_Image_AI')
Traceback (most recent call last):
File "F:\Google Drive\DCM_Image_AI\untitled1.py", line 85, in <module>
plt.imshow(x_train[i], cmap=plt.cm.binary)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\pyplot.py", line 2677, in imshow
None else {}), **kwargs)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\__init__.py", line 1599, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\axes\_axes.py", line 5679, in imshow
im.set_data(X)
File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\image.py", line 690, in set_data
.format(self._A.shape))
TypeError: Invalid shape (3072,) for image data
 anyone can kindly help me out with this. many thanks.
Upvotes: 2
Views: 9123
Reputation: 466
First thing I realize you are dividing your pixel values with 255. Comment these lines.
x_train /= 255.0
x_test /= 255.0
After that reshape your image like that
np.reshape(image, (32, 32, 3))
this should work.
Upvotes: 1