I'm trying to load this data set https://github.com/jaddoescad/ants_and_bees
However there is a error when I try to iterate the data loader
training_dataset = datasets.ImageFolder('ants_and_bees/train', transform=transform_train)
validation_dataset = datasets.ImageFolder('ants_and_bees/val', transform=transform)
training_loader = torch.utils.data.DataLoader(training_dataset, batch_size=20, shuffle=True)
validation_loader = torch.utils.data.DataLoader(validation_dataset, batch_size = 20, shuffle=False)
def im_convert(tensor):
image = tensor.cpu().clone().detach().numpy()
image = image.transpose(1, 2, 0)
image = image * np.array((0.5, 0.5, 0.5)) + np.array((0.5, 0.5, 0.5))
image = image.clip(0, 1)
return image
classes = ('ant', 'bee')
dataiter = iter(training_loader)
images, labels = next(dataiter)
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(20):
ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])
plt.imshow(im_convert(images[idx]))
ax.set_title(classes[labels[idx].item()])
The error message doesn't help much, I read some similar problems here, but couldn't find a solution.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-58-fb882084a0d1> in <module>
1 dataiter = iter(training_loader)
----> 2 images, labels = next(dataiter)
3 fig = plt.figure(figsize=(25, 4))
4
5 for idx in np.arange(20):
10 frames
/usr/local/lib/python3.8/dist-packages/PIL/TgaImagePlugin.py in _open(self)
64 flags = i8(s[17])
65
---> 66 self.size = i16(s[12:]), i16(s[14:])
67
68 # validate header fields
AttributeError: can't set attribute
The code is from this Pytorch tutorial https://github.com/rslim087a/PyTorch-for-Deep-Learning-and-Computer-Vision-Course-All-Codes-/blob/master/PyTorch%20for%20Deep%20Learning%20and%20Computer%20Vision%20Course%20(All%20Codes)/Transfer_Learning.ipynb
I'm running on Google Colab.
OBS: This seems to be a Colab problem or the python version there.
I was able to run locally with Python 3.9.13 environment.
Add this please to your code :
transform_train = transforms.Compose([
transforms.ToTensor()
])
Related
I'm new to pytorch. I'm trying to create a DCGAN project. I used the entire official pytorch tutorial as a base.
I have a numpy array that is the combination of eight arrays, which given a shape (60,60,8) this shape is special
lista2 = [0, 60, 120, 180, 240, 300, 360, 420]
total = []
for i in lista2:
N1 = intesity[0:60, i:i+60]
total.append(N1)
N2 = intesity[60:120, i:i+60]
total.append(N2)
N3 = intesity[120:180, i:i+60]
total.append(N3)
N4 = intesity[180:240, i:i+60]
total.append(N4)
N5 = intesity[240:300, i:i+60]
total.append(N5)
N6 = intesity[300:360, i:i+60]
total.append(N6)
N7 = intesity[360:420, i:i+60]
total.append(N7)
N8 = intesity[420:480, i:i+60]
total.append(N8)
total = np.reshape(total, (64, 60,60,8))
total -= total.min()
total /= total.max()
total = np.asarray(total)
print(np.shape(total)
(64, 60, 60, 8)
as you can see there are 64 elements in that array, there are 64 training images (very few for now), this array is converted to a tensor and then to a pytorch dataset
tensor_c = torch.tensor(total)
creating a dataset and a dataloader I get the following error, when trying to graph the training images of this DCGAN
dataset = TensorDataset(tensor_c) # create your datset
dataloader = DataLoader(dataset) # create your dataloader
real_batch = next(iter(dataloader))
plt.figure(figsize=(16,16))
plt.axis("off")
plt.title("Training Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=0, normalize=True).cpu(),(1,2,0)))
dataset_size = len(dataloader.dataset)
dataset_size
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-42-5ba2d666ef25> in <module>()
10 plt.axis("off")
11 plt.title("Training Images")
---> 12 plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=0, normalize=True).cpu(),(1,2,0)))
13 dataset_size = len(dataloader.dataset)
14 dataset_size
5 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
697 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
698 raise TypeError("Invalid shape {} for image data"
--> 699 .format(self._A.shape))
700
701 if self._A.ndim == 3:
TypeError: Invalid shape (60, 60, 8) for image data
I am too new to Pytorch I would like to know how I can solve this problem
Images are expected to be stored as arrays of the form height x width x n_channels in general, where n_channels is 3 for a standard RGB image, or in some cases 4 for an RGBA image. matplotlib doesn't have a built-in understanding of how to plot an image with 8 channels, as your image data currently has.
Also pay attention to the ordering of the dimensions, as pytorch expects images of the form batch_idx x channel x height x width which is convenient for applying 2D convolutions because they can be strided across the last 2 dimensions. Be careful to do the conversion to pytorch form after trying to plot the images in matplotlib form.
IMAGE_RES = 224
def format_image(image, label):
image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))/255.0
return image, label
BATCH_SIZE = 32
train_batches = train_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
train_gray_batches = train_grey_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
test_batches = test_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
test_grey_batches = test_grey_dataset.map(format_image).batch(BATCH_SIZE).prefetch(1)
----------
threshold = 100.0
dropoutrate = 0.5
n_outchannels = 3
height, width = IMAGE_RES, IMAGE_RES
def max_norm_regularizer(threshold, axes=None, name="max_norm",
collection="max_norm"):
def max_norm(weights):
clipped = tf.clip_by_norm(weights, clip_norm=threshold, axes=axes)
clip_weights = tf.assign(weights, clipped, name=name)
tf.add_to_collection(collection, clip_weights)
return None # there is no regularization loss term
return max_norm
max_norm_reg = max_norm_regularizer(threshold=threshold)
clip_all_weights = tf.compat.v1.get_collection("max_norm")
----------
def leaky_relu(z,name=None):
return tf.maximum(0.5*z,z,name=name)
from functools import partial
he_init = tf.keras.initializers.VarianceScaling()
----------
X = tf.compat.v1.placeholder(shape=(None,width,height,2),dtype=tf.float32)
print(X)
training = tf.compat.v1.placeholder_with_default(False,shape=(),name='training')
X_drop = tf.keras.layers.Dropout(X,dropoutrate)
my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization,training=training,momentum=0.9)
bn0 = my_batch_norm_layer(X_drop)
bn0_act = leaky_relu(bn0)
print(bn0_act)
This error creates in my program. what is the problem I don't
understand to solve this I search many times but not solve this
problem?
Tensor("Placeholder_26:0", shape=(?, 224, 224, 2), dtype=float32)
TypeError Traceback (most recent call last)
<ipython-input-64-adf525e2e2de> in <module>()
5 X_drop = tf.keras.layers.Dropout(X,dropoutrate)
6 my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization,training=training,momentum=0.9)
----> 7 bn0 = my_batch_norm_layer(X_drop)
8 bn0_act = leaky_relu(bn0)
9 print(bn0_act)
4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/generic_utils.py
in validate_kwargs(kwargs, allowed_kwargs, error_message)
1135 for kwarg in kwargs:
1136 if kwarg not in allowed_kwargs:
-> 1137 raise TypeError(error_message, kwarg)
1138
1139
TypeError: ('Keyword argument not understood:', 'training')
You need to put the arguments inside brackets since the training keyword is currently being applied to partial(). You also want to use trainable instead of training (I'm assuming you want to freeze the batchnorm layer).
X = tf.compat.v1.placeholder(shape=(None,width,height,2),dtype=tf.float32)
print(X)
training = tf.compat.v1.placeholder_with_default(False,shape=(),name='training')
X_drop = tf.keras.layers.Dropout(dropoutrate)(X)
my_batch_norm_layer = partial(tf.keras.layers.BatchNormalization(trainable=training,momentum=0.9))
bn0 = my_batch_norm_layer(X_drop)
bn0_act = leaky_relu()(bn0)
print(bn0_act)
I am trying to make a bar graph with Anaconda/Python 3 with the following code but I keep on getting an error. I first read a dataset that has been formatted to be an excel/csv file and that works fine because I am able to make correlation plots as well as some other plots successfully. I tried the same code with both an excel file and csv file that contain the same data and I get the same error. When the program attempts to execute the line responsible for making a bar graph, I get the following error:
KeyError Traceback (most recent call last)
<ipython-input-4-b72668216a77> in <module>
64
65 # Compare mean and standard deviation between attributes
---> 66 compare = dataset.groupby("target_class")[['mean_profile', 'std_profile', 'kurtosis_profile', 'skewness_profile', 'mean_dmsnr_curve', 'std_dmsnr_curve', 'kurtosis_dmsnr_curve', 'skewness_dmsnr_curve']].mean().reset_index()
67 # compare = dataset.groupby("target_class")[['mean_profile', 'std_profile', 'kurtosis_profile', 'skewness_profile', 'mean_dmsnr_curve', 'std_dmsnr_curve', 'kurtosis_dmsnr_curve', 'skewness_dmsnr_curve']].mean().reset_index()
68
~\Anaconda3\lib\site-packages\pandas\core\base.py in __getitem__(self, key)
263 bad_keys = list(set(key).difference(self.obj.columns))
264 raise KeyError("Columns not found: {missing}"
--> 265 .format(missing=str(bad_keys)[1:-1]))
266 return self._gotitem(list(key), ndim=2)
267
KeyError: "Columns not found: 'mean_dmsnr_curve', 'std_profile', 'std_dmsnr_curve', 'skewness_dmsnr_curve', 'mean_profile', 'kurtosis_profile', 'skewness_profile', 'kurtosis_dmsnr_curve'"
Other plots work fine but the comparison bar graph plot does not. Here is the code that I am trying to execute:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import itertools
warnings.filterwarnings("ignore")
#matplotlib inline
print("This is a test right before excel file is read")
# Utilize the pandas library to read the data
dataset = pd.read_csv(r'C:\userpath\Machine Learning Project\pulsar_stars_test.csv')
#dataset = pd.read_excel(r'C:\userpath\Machine Learning Project\pulsar_stars_test.xlsx')
# Print the number of rows and columns that the data has to the user
print("This is the number of rows: ", dataset.shape[0])
print("This is the number of columns: ", dataset.shape[1])
# Use pandas to print out the information about the data
print("This is the data information: ", dataset.info())
# Use pandas to display information about missing data
print("This is the missing data: ", dataset.isnull().sum())
# Make a figure appear to display a dataset summary to the user
plt.figure(figsize = (12, 8))
sns.heatmap(dataset.describe()[1:].transpose(), annot = True, linecolor = "w", linewidth = 2, cmap = sns.color_palette("Set2"))
plt.title("Data Summary")
plt.show()
# Instantiate another figure to display some correlation data to the user
correlation = dataset.corr()
plt.figure(figsize = (10, 8))
sns.heatmap(correlation, annot = True, cmap = sns.color_palette("magma"), linewidth = 2, edgecolor = "k")
plt.title("CORRELATION BETWEEN VARIABLES")
plt.show()
# Compute the proportion of each target variabibble in the dataset
plt.figure(figsize = (12, 6))
plt.subplot(121)
ax = sns.countplot(y = dataset["target_class"], palette = ["r", "g"], linewidth = 1, edgecolor = "k"*2)
for i, j in enumerate(dataset["target_class"].value_counts().values):
ax.text(.7, i, j, weight = "bold", fontsize = 27)
plt.title("Count for target variable in dataset")
plt.subplot(122)
plt.pie(dataset["target_class"].value_counts().values, labels = ["not pulsar stars", "pulsar stars"], autopct = "%1.0f%%", wedgeprops = {"linewidth":2, "edgecolor":"white"})
#plt.pie(data["target_class"].value_counts().values, labels = ["not pulsar stars", "pulsar stars"], autopct = "%1.0f%%", wedgeprops = {"linewidth":2, "edgecolor":"white"})
my_circ = plt.Circle((0,0), .7, color = "white")
plt.gca().add_artist(my_circ)
plt.subplots_adjust(wspace = .2)
plt.title("Proportion of target variabibble in dataset")
plt.show()
# Compare mean and standard deviation between attributes
compare = dataset.groupby("target_class")[['mean_profile', 'std_profile', 'kurtosis_profile', 'skewness_profile', 'mean_dmsnr_curve', 'std_dmsnr_curve', 'kurtosis_dmsnr_curve', 'skewness_dmsnr_curve']].mean().reset_index()
# compare = dataset.groupby("target_class")[['mean_profile', 'std_profile', 'kurtosis_profile', 'skewness_profile', 'mean_dmsnr_curve', 'std_dmsnr_curve', 'kurtosis_dmsnr_curve', 'skewness_dmsnr_curve']].mean().reset_index()
compare = compare.drop("target_class", axis = 1)
compare.plot(kind = "bar", width = 0.6, figsize = (13,6), colormap = "Set2")
plt.grid(True, alpha = 0.3)
plt.title("COMPARING MEAN OF ATTRIBUTES FOR TARGET CLASSES")
# Second comparison plot
compare1.dataset.groupby("target_class")[['mean_profile', 'std_profile', 'kurtosis_profile', 'mean_dmsnr_curve', 'std_dmsnr_curve','kurtosis_dmsnr_curve','skewness_dmsnr_curve']].mean().reset_index()
compare1 = compare1.drop("target_class", axis = 1)
compare1.plot(kind = "bar", width = 0.6, figsize = (13, 6), colormap = "Set2")
plt.grid(True, alpha = 0.3)
plt.title("COMPARING STANDARD DEVIATION OF ATTRIBUTES FOR TARGET CLASSES")
plt.show()
The program fails when it hits the line (line 66) at which the groupby function is called for the first time in the program. Does anybody know how to fix this?
Plz update your version of pandas ie package whatever u r using....plz update that...then and then key error will be solve
When loading images in, I'm trying to make sure they are loaded in correctly by printing them out in pyplot, but I'm having problems. How do I load these images into Tensorflow and check them with pyplot's imshow() (or some other way)?
The image data is a one-channel(black and white) jpeg. It is initially loaded as a Tensor with an unknown shape and an uint8 dtype. I've tried making sure to reshape the Tensor into the correct shape and casting to float32. I've also tried making sure the values scaled from 0.0 - 1.0 as a float and using the Grey cmapping within the imshow() function.
import tensorflow as tf
import matplotlib.pyplot as plt
def load_and_preprocess_jpeg(imagepath):
img = tf.read_file(imagepath)
img_tensor = tf.image.decode_jpeg(img)
img_tensor.set_shape([792,1224,1])
img_tensor = tf.reshape(img_tensor, [792,1224])
img_tensor = tf.cast(img_tensor, tf.float32, name='ImageCast')
#img_tensor /= 255.0 #Tried with and without
return img_tensor
def read_data(all_filenames):
path_Dataset = tf.data.Dataset.from_tensor_slices(all_filenames)
image_Dataset = path_Dataset.map(load_and_preprocess_jpeg)
plt.figure(figsize=(8,8))
temp_DS = image_Dataset.take(4)
itera = temp_DS.make_one_shot_iterator()
for n in range(4):
image = itera.get_next()
plt.subplot(2,2,n+1)
plt.imshow(image)
plt.grid(False)
plt.xticks([])
plt.yticks([])
My stack trace:
File "<stdin>", line 1, in <module>
line 34, in read_data
plt.imshow(image)
matplotlib\pyplot.py, line 3205, in imshow
**kwargs)
matplotlib\__init__.py, line 1855, in inner
return func(ax, *args, **kwargs)
matplotlib\axes\_axes.py, line 5487, in imshow
im.set_data(X)
matplotlib\image.py, line 649, in set_data
raise TypeError("Image data cannot be converted to float")
You are trying to plot tensors. In order to plot images, you have to run the session first. Try the following code:
import tensorflow as tf
import matplotlib.pyplot as plt
def load_and_preprocess_jpeg(imagepath):
img = tf.read_file(imagepath)
img_tensor = tf.image.decode_jpeg(img)
img_tensor = tf.image.resize_images(img_tensor, [img_size,img_size])
img_tensor = tf.cast(img_tensor, tf.float32, name='ImageCast')
img_tensor /= 255.0
return img_tensor
path_Dataset = tf.data.Dataset.from_tensor_slices(all_filenames)
image_Dataset = path_Dataset.map(load_and_preprocess_jpeg)
temp_DS = image_Dataset.take(4)
itera = temp_DS.make_one_shot_iterator()
image = itera.get_next()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
while True:
try:
image_to_plot = sess.run(image)
plt.figure(figsize=(8,8))
plt.subplot(2,2,n+1)
plt.imshow(image_to_plot)
plt.grid(False)
plt.xticks([])
plt.yticks([])
except tf.errors.OutOfRangeError:
break
I am trying to use Caffe.Classifier class and its predict() method on my Imagenet trained caffemodel.
Images were resized to 256x256 and crops of 227x227 were used to train the net.
Everything is simple and straight forward, yet I keep getting weird errors such as the following :
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-7-3b440ebf1f6e> in <module>()
17 image_dims=(256, 256))
18
---> 19 out = net.predict([image_caffe], oversample=True)
20 print(labels[out[0].argmax()].strip(),' (', out[0][out[0].argmax()] , ')')
21 plabel = int(labels[out[0].argmax()].strip())
<ipython-input-5-e6ae1810b820> in predict(self, inputs, oversample)
65 for ix, in_ in enumerate(inputs):
66 print('image dims = ',self.image_dims[0],',',self.image_dims[1] ,'_in = ',in_.shape)
---> 67 input_[ix] = caffe.io.resize_image(in_, self.image_dims)
68
69 if oversample:
C:\Users\Master\Anaconda3\envs\anaconda35\lib\site-packages\caffe\io.py in resize_image(im, new_dims, interp_order)
335 # ndimage interpolates anything but more slowly.
336 scale = tuple(np.array(new_dims, dtype=float) / np.array(im.shape[:2]))
--> 337 resized_im = zoom(im, scale + (1,), order=interp_order)
338 return resized_im.astype(np.float32)
339
C:\Users\Master\Anaconda3\envs\anaconda35\lib\site-packages\scipy\ndimage\interpolation.py in zoom(input, zoom, output, order, mode, cval, prefilter)
588 else:
589 filtered = input
--> 590 zoom = _ni_support._normalize_sequence(zoom, input.ndim)
591 output_shape = tuple(
592 [int(round(ii * jj)) for ii, jj in zip(input.shape, zoom)])
C:\Users\Master\Anaconda3\envs\anaconda35\lib\site-packages\scipy\ndimage\_ni_support.py in _normalize_sequence(input, rank, array_type)
63 if len(normalized) != rank:
64 err = "sequence argument must have length equal to input rank"
---> 65 raise RuntimeError(err)
66 else:
67 normalized = [input] * rank
RuntimeError: sequence argument must have length equal to input rank
And here is the snippets of code I'm using :
import sys
import caffe
import numpy as np
import lmdb
import matplotlib.pyplot as plt
import itertools
def flat_shape(x):
"Returns x without singleton dimension, eg: (1,28,28) -> (28,28)"
return x.reshape(x.shape)
def db_reader(fpath, type='lmdb'):
if type == 'lmdb':
return lmdb_reader(fpath)
else:
return leveldb_reader(fpath)
def lmdb_reader(fpath):
import lmdb
lmdb_env = lmdb.open(fpath)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
for key, value in lmdb_cursor:
datum = caffe.proto.caffe_pb2.Datum()
datum.ParseFromString(value)
label = int(datum.label)
image = caffe.io.datum_to_array(datum).astype(np.uint8)
yield (key, flat_shape(image), label)
def leveldb_reader(fpath):
import leveldb
db = leveldb.LevelDB(fpath)
for key, value in db.RangeIter():
datum = caffe.proto.caffe_pb2.Datum()
datum.ParseFromString(value)
label = int(datum.label)
image = caffe.io.datum_to_array(datum).astype(np.uint8)
yield (key, flat_shape(image), label)
Classifier class (copied form Caffe's python directory):
import numpy as np
import caffe
class Classifier(caffe.Net):
"""
Classifier extends Net for image class prediction
by scaling, center cropping, or oversampling.
Parameters
----------
image_dims : dimensions to scale input for cropping/sampling.
Default is to scale to net input size for whole-image crop.
mean, input_scale, raw_scale, channel_swap: params for
preprocessing options.
"""
def __init__(self, model_file, pretrained_file, image_dims=None,
mean=None, input_scale=None, raw_scale=None,
channel_swap=None):
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
# configure pre-processing
in_ = self.inputs[0]
print('inputs[0]',self.inputs[0])
self.transformer = caffe.io.Transformer(
{in_: self.blobs[in_].data.shape})
self.transformer.set_transpose(in_, (2, 0, 1))
if mean is not None:
self.transformer.set_mean(in_, mean)
if input_scale is not None:
self.transformer.set_input_scale(in_, input_scale)
if raw_scale is not None:
self.transformer.set_raw_scale(in_, raw_scale)
if channel_swap is not None:
self.transformer.set_channel_swap(in_, channel_swap)
print('crops: ',self.blobs[in_].data.shape[2:])
self.crop_dims = np.array(self.blobs[in_].data.shape[2:])
if not image_dims:
image_dims = self.crop_dims
self.image_dims = image_dims
def predict(self, inputs, oversample=True):
"""
Predict classification probabilities of inputs.
Parameters
----------
inputs : iterable of (H x W x K) input ndarrays.
oversample : boolean
average predictions across center, corners, and mirrors
when True (default). Center-only prediction when False.
Returns
-------
predictions: (N x C) ndarray of class probabilities for N images and C
classes.
"""
# Scale to standardize input dimensions.
input_ = np.zeros((len(inputs),
self.image_dims[0],
self.image_dims[1],
inputs[0].shape[2]),
dtype=np.float32)
for ix, in_ in enumerate(inputs):
print('image dims = ',self.image_dims[0],',',self.image_dims[1] ,'_in = ',in_.shape)
input_[ix] = caffe.io.resize_image(in_, self.image_dims)
if oversample:
# Generate center, corner, and mirrored crops.
input_ = caffe.io.oversample(input_, self.crop_dims)
else:
# Take center crop.
center = np.array(self.image_dims) / 2.0
crop = np.tile(center, (1, 2))[0] + np.concatenate([
-self.crop_dims / 2.0,
self.crop_dims / 2.0
])
input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]
# Classify
caffe_in = np.zeros(np.array(input_.shape)[[0, 3, 1, 2]],
dtype=np.float32)
for ix, in_ in enumerate(input_):
caffe_in[ix] = self.transformer.preprocess(self.inputs[0], in_)
out = self.forward_all(**{self.inputs[0]: caffe_in})
predictions = out[self.outputs[0]]
# For oversampling, average predictions across crops.
if oversample:
predictions = predictions.reshape((len(predictions) / 10, 10, -1))
predictions = predictions.mean(1)
return predictions
Main section :
proto ='deploy.prototxt'
model='snap1.caffemodel'
mean='imagenet_mean.binaryproto'
db_path='G:/imagenet/ilsvrc12_val_lmdb'
# Extract mean from the mean image file
#mean_blobproto_new = caffe.proto.caffe_pb2.BlobProto()
#f = open(mean, 'rb')
#mean_blobproto_new.ParseFromString(f.read())
#mean_image = caffe.io.blobproto_to_array(mean_blobproto_new)
#f.close()
mu = np.load('mean.npy').mean(1).mean(1)
caffe.set_mode_gpu()
reader = lmdb_reader(db_path)
i = 0
for i, image, label in reader:
image_caffe = image.reshape(1, *image.shape)
print(image_caffe.shape, mu.shape)
net = Classifier(proto, model,
mean= mu,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
out = net.predict([image_caffe], oversample=True)
print(i, labels[out[0].argmax()].strip(),' (', out[0][out[0].argmax()] , ')')
i+=1
What is wrong here?
I found the cause, I had to feed the image in the form of 3D tensor not a 4D one!
so our 4d tensor:
image_caffe = image.reshape(1, *image.shape)
needed to be changed to a 3D one:
image_caffe = image.transpose(2,1,0)
As a side note, try using python2 for running any caffe related. python3 might work at first but will definitely cause a lot of headaches. for instance, predict method with oversample set to True, will crash under python3 but works just fine under python2!