运行大豆杂草分类器时,应该在该图中显示图像,但它们显示为灰色。不知道从这里去哪里。 输出应该是这样的:
代码实际上是这样的:
代码:
1。
import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (19.0, 17.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
2。
data_dir = 'home/jack/Desktop/weed-detection-in-soybean-crops/dataset/dataset/'
classes = ['broadleaf', 'grass', 'soil', 'soybean']
num_file = 1100
all_files = []
num_data =num_file*len(classes)
Y = np.zeros(num_data)
for i, cls in enumerate(classes):
all_files += [f for f in glob.glob(data_dir+cls+'/*.tif')][:num_file]
Y[i*num_file:(i+1)*num_file] = i # label all classes with int [0.. len(classes)]
# Image dimension
im_width = 200
im_height = 200
im_channel = 3
dim = im_width * im_height * im_channel
X = np.ndarray(shape=(num_data, im_width, im_height, im_channel), dtype=np.float64)
for idx, file in enumerate(all_files):
X[idx] = cv2.resize(cv2.imread(file), (im_width, im_height))
X_train = np.empty(shape=(4000,im_width, im_height, im_channel), dtype=np.float64)
X_val = np.empty(shape=(200,im_width, im_height, im_channel), dtype=np.float64)
X_test = np.empty(shape=(200,im_width, im_height, im_channel), dtype=np.float64)
y_train = np.empty(4000)
y_val = np.empty(200)
y_test = np.empty(200)
for i, cls in enumerate(classes):
X_test[50*i:50*(i+1)] = X[np.where(Y == i)[0][:50]]
X_val[50*i:50*(i+1)] = X[np.where(Y == i)[0][50:100]]
X_train[1000*i:1000*(i+1)] = X[np.where(Y == i)[0][100:]]
y_test[50*i:50*(i+1)] = i
y_val[50*i:50*(i+1)] = i
y_train[1000*i:1000*(i+1)] = i
del Y
del X
# Extract features
#Shuffle training index
train_idxs = np.random.permutation(X_train.shape[0])
y_train = y_train[train_idxs].astype(int)
X_train = X_train[train_idxs]
X_train = np.reshape(X_train, (X_train.shape[0], -1)).astype('float64')
X_test = np.reshape(X_test, (X_test.shape[0], -1)).astype('float64')
X_val = np.reshape(X_val, (X_val.shape[0], -1)).astype('float64')
X_tiny = X_train[100:110].astype('float64')
y_tiny = y_train[100:110].astype(int)
num_dev = 500
X_dev = X_train[0:num_dev].astype('float64')
y_dev = y_train[0:num_dev].astype(int)
print("X_train shape", X_train.shape, "| y_train shape:", y_train.shape)
print("X_test shape", X_test.shape, "| y_test shape:", y_test.shape)
print("X_val shape", X_val.shape, "| y_val shape:", y_val.shape)
print("X_dev shape", X_dev.shape, "| y_dev shape:", y_dev.shape)
print("X_tiny shape", X_tiny.shape, "| y_tiny shape:", y_tiny.shape)
#Subtract out the mean image
#first: compute the mean image
# mean_image = np.mean(X_train, axis=0) #axis=0. stack horizontally
mean_image = 128
#Second subtract the mean image from train and test data
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
X_dev -= mean_image
X_tiny -= mean_image
#Third append the bias dimension using linear algebra trick
#Not for net
# X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
# X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))])
# X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
# X_dev = np.hstack([X_dev, np.ones((X_dev.shape[0], 1))])
# X_tiny = np.hstack([X_tiny, np.ones((X_tiny.shape[0], 1))])
print('=====STACK BIAS term=====')
print("X_train shape", X_train.shape)
print("X_test shape", X_test.shape)
print("X_val shape", X_val.shape)
print("X_dev shape", X_dev.shape)
print("X_tiny shape", X_tiny.shape)
3。
# Visualize some images
# Make sure that everything when OK
classes = ['broadleaf', 'grass', 'soil', 'soybean']
n_class = len(classes)
samples_per_class = 4
for y, cls in enumerate(classes):
idxes = np.flatnonzero(y == y_train)
idxes = np.random.choice(idxes, samples_per_class, replace = False)
for i, idx in enumerate(idxes):
plt_idx = i * n_class + y + 1
plt.subplot(samples_per_class,n_class, plt_idx)
plt.imshow(X_train[idx].reshape(im_width, im_height, im_channel).astype('uint8'))
if(i==0): plt.title(cls)
plt.show()
任何帮助将不胜感激。我已经看了很多遍,但是找不到任何引用此内容的东西。谢谢。
答案 0 :(得分:0)
进行绘图时,将dtype更改为uint8
...您尝试不这样做吗?如果它们是小浮点数,那么您将得到很多零。
换句话说,尝试将倒数第三行更改为:
plt.imshow(X_train[idx].reshape(im_width, im_height, im_channel))\
答案 1 :(得分:0)
您需要正确传递“ data_dir”路径。而不是将路径设置为
data_dir = 'home/jack/Desktop/weed-detection-in-soybean-crops/dataset/dataset/'
使用
data_dir = r'/home/jack/Desktop/weed-detection-in-soybean-crops/dataset/dataset/'
获取正确的路径类型“ pwd”。