我是 pytorch 的新手,现在我被困在这一个多星期了。我正在尝试使用 AlexNet 制作“gta san Andreas”自动驾驶汽车,但在准备数据时遇到了很多问题。现在我收到这个错误。
Traceback (most recent call last):
File "training_script.py", line 19, in <module>
transformed_data = transform(all_data)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 49, in __call__
img = t(img)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 76, in __call__
return F.to_tensor(pic)
File "C:\Users\Mukhtar\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 48, in to_tensor
img = torch.from_numpy(pic.transpose((2, 0, 1)))
ValueError: axes don't match array
这是训练脚本
from AlexNetPytorch import*
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.utils.data
import numpy as np
import torch
AlexNet = AlexNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(AlexNet.parameters(), lr=0.001, momentum=0.9)
all_data = np.load('training_data.npy')
transform = transforms.Compose([
# you can add other transformations in this list
transforms.ToTensor()
])
transformed_data = transform(all_data)
# # data_set = torchvision.datasets.ImageFolder('training_data.npy' ,transform = transforms.ToTensor() )
data_loader = torch.utils.data.DataLoader(training_data, batch_size=4,shuffle=True, num_workers=2)
# training_data = all_data[:-500]lk
# testing_data = all_data[-500:]
if __name__ == '__main__':
for epoch in range(8):
runing_loss = 0.0
for i,data in enumerate(data_loader , 0):
inputs= data[0]
inputs = torch.FloatTensor(inputs)
labels= data[1]
labels = torch.FloatTensor(labels)
optimizer.zero_grad()
outputs = AlexNet(inputs)
loss = criterion(outputs , labels)
loss.backward()
optimizer.step()
runing_loss +=loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('finished')
这就是我准备数据的方式
import cv2
from PIL import ImageGrab
import numpy as np
import time
from directKeys import PressKey,W,A,S,D
from getKeys import key_check
import os
def keys_to_output(keys):
output = [0,0,0]
if 'A' in keys:
output[0] = 1
elif 'D' in keys:
output[2] = 1
else:
output[1] = 1
return output
file_name = "training_data.npy"
if os.path.isfile(file_name):
print("file exists , loading previous data!")
training_data = list(np.load(file_name))
else:
print("file does not exist , starting fresh")
training_data = []
last_time = time.time()
while True:
kernel = np.ones((15 , 15) , np.float32)/225
get_screen = ImageGrab.grab(bbox=(10,10,1280,720))
screen_shot = np.array(get_screen)
hsv = cv2.cvtColor(screen_shot , cv2.COLOR_BGR2HSV)
lower_color = np.array([90 , 0 , 70])
upper_color = np.array([100 , 100 , 100])
output = cv2.inRange(hsv , lower_color , upper_color)
kernel = np.ones((1,20), np.uint8) # note this is a horizontal kernel
dilation = cv2.dilate(output, kernel, iterations=1)
output = cv2.erode(dilation, kernel, iterations=1)
# output = cv2.Canny(output , threshold1 = 50 , threshold2 = 300)
# output = cv2.GaussianBlur(output , (15,15) , 0)
resized = cv2.resize(output , (640 , 480))
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
cv2.imshow('manipulated' , resized)
screen_output = cv2.resize(output , (32 ,32))
keys = key_check()
Keys_output = keys_to_output(keys)
training_data.append([screen_output,Keys_output])
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
if len(training_data) % 500 == 0:
print(len(training_data))
np.save(file_name,training_data)
我尝试了很多解决方案,但没有一个有效,但我觉得我错过了一些东西。我太忙了,所以请帮忙