在matplotlib中显示张量图像

时间:2019-03-10 01:13:41

标签: python matplotlib pytorch

我正在为Udacity的具有Python纳米度的AI进行项目。

我正在尝试显示从图像文件路径获得的torch.cuda.FloatTensor。在该图像下方将显示一个条形图,显示最可能出现的前5个花名及其相关概率。

@PersistenceContext
private EntityManager entityManager = 
entityManagerFactory.createEntityManager();

imshow功能的赋予方式为

buildscript {
repositories {
    jcenter()
    mavenCentral()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'com.google.gms:google-services:4.2.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

但是我得到了这个输出

plt.figure(figsize=(3,3))
path = 'flowers/test/1/image_06743.jpg' 

top5_probs, top5_class_names = predict(path, model,5)

print(top5_probs)
print(top5_class_names)

flower_np_image = process_image(Image.open(path))
flower_tensor_image = torch.from_numpy(flower_np_image).type(torch.cuda.FloatTensor)
flower_tensor_image = flower_tensor_image.unsqueeze_(0)

axs = imshow(flower_tensor_image, ax = plt)
axs.axis('off')
axs.title(top5_class_names[0])
axs.show()
fig, ax = plt.subplots()
y_pos = np.arange(len(top5_class_names))
plt.barh(y_pos, list(reversed(top5_probs)))
plt.yticks(y_pos, list(reversed(top5_class_names)))
plt.ylabel('Flower Type')
plt.xlabel('Class Probability')

我的预测函数有效,但是imshow只是因转置调用而阻塞。有想法该怎么解决这个吗?我认为它模糊地与转换回一个numpy数组有关。

我正在使用的笔记本可以在https://github.com/BozSteinkalt/ImageClassificationProject

找到

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在尝试将numpy.transpose应用于package main import ( "github.com/facebookgo/inject" ) type BookService struct { Database Database `inject:""` } type Database struct { ConnectionString string `inject:""` } func main() { var graph inject.Graph var service BookService _ = graph.Provide(&inject.Object{Value: &service}, &inject.Object{Value: Database{ConnectionString: "uri"}}) _ = graph.Populate() println(service.Database.ConnectionString) } 对象,从而调用tensor.transpose

您应该先使用.numpy()torch.Tensor转换为numpy

flower_tensor_image
相关问题