首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python :在SageMaker中加载图像的问题

Python :在SageMaker中加载图像的问题
EN

Stack Overflow用户
提问于 2020-12-02 15:45:00
回答 1查看 3.7K关注 0票数 0

我使用Tifffile加载..tiff图像文件,然后将它们转换为PIL映像来处理它们。我使用这个解决方法是因为其他方法在一些特定的TIFF图像上有问题。在我的本地机器上,以下代码运行良好。但是,当我在SageMaker环境中运行代码时,它不能工作。

代码语言:javascript
运行
复制
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import scipy.ndimage
import os
import random
import tifffile
from PIL import Image
from PIL.ExifTags import TAGS
import matplotlib
#import imagecodecs
# Causes errors with some pictures
#image = np.expand_dims(scipy.ndimage.imread(image_path), 0)
    
# Causes errors with some pictures
#image = np.expand_dims(matplotlib.pyplot.imread(image_path), 0)
    
# This works on my local machine, but not in Amazon SageMaker
# Use tifffile to load the image
img = tifffile.imread(image_path)
# Make into "PIL Image" and carry on as usual
image = Image.fromarray(img)

我得到以下错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-137-fe721f47a3dc> in <module>
----> 1 batch_image_augmentation(test_images, 20)

<ipython-input-134-b0ca28d40a9c> in batch_image_augmentation(path_list, n)
      3         target_dir = "./Test/" + path[:-5] + "_AUG"
      4         print(path)
----> 5         augment_image(path, target_dir, n)

<ipython-input-131-cdef2a00cd5f> in augment_image(image_path, target_dir, n)
     24     #Some images cause problems, so I try the same workaround as in the PNG-conversion script
     25     # Use tifffile to load the image
---> 26     img = tifffile.imread(image_path)
     27     # Make into "PIL Image" and carry on as usual
     28     image = Image.fromarray(img)

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in imread(files, **kwargs)
    719         if isinstance(files, (str, os.PathLike)) or hasattr(files, 'seek'):
    720             with TiffFile(files, **kwargs_file) as tif:
--> 721                 return tif.asarray(**kwargs)
    722 
    723     with TiffSequence(files, **kwargs_seq) as imseq:

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in asarray(self, key, series, level, out, maxworkers)
   2805                     typecode, product(series.shape), out=out)
   2806         elif len(pages) == 1:
-> 2807             result = pages[0].asarray(out=out, maxworkers=maxworkers)
   2808         else:
   2809             result = stack_pages(pages, out=out, maxworkers=maxworkers)

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in asarray(self, out, squeeze, lock, reopen, maxworkers)
   5646 
   5647             for _ in self.segments(
-> 5648                 func=func, lock=lock, maxworkers=maxworkers, sort=True
   5649             ):
   5650                 pass

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in segments(self, lock, maxworkers, func, sort)
   5510                 *self._offsetscounts, lock=lock, sort=sort, flat=True
   5511             ):
-> 5512                 yield decode(segment)
   5513         else:
   5514             # reduce memory overhead by processing chunks of up to

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in decode(args, decodeargs, keyframe, func)
   5499 
   5500         def decode(args, decodeargs=decodeargs, keyframe=keyframe, func=func):
-> 5501             result = keyframe.decode(*args, **decodeargs)
   5502             if func is not None:
   5503                 return func(result)

/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in decode(exc, *args, **kwargs)
   5228         except KeyError as exc:
   5229             def decode(*args, exc=str(exc)[1:-1], **kwargs):
-> 5230                 raise ValueError(f'TiffPage {self.index}: {exc}')
   5231             return cache(decode)
   5232 
ValueError: TiffPage 0: <COMPRESSION.LZW: 5> requires the 'imagecodecs' package

当我尝试安装imagecodecs时,pip告诉我它已经安装了:

代码语言:javascript
运行
复制
bash-4.2$ pip install imagecodecs
Requirement already satisfied: imagecodecs in /opt/conda/lib/python3.7/site-packages (2020.5.30)
Requirement already satisfied: numpy>=1.15.1 in /opt/conda/lib/python3.7/site-packages (from imagecodecs) (1.19.4)
bash-4.2$

但是,无论如何,如果我在导入中添加了导入映像集,则会得到以下错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-138-920d3e93091b> in <module>
      8 from PIL.ExifTags import TAGS
      9 import matplotlib
---> 10 import imagecodecs
     11 
     12 # https://gis.stackexchange.com/questions/365950/how-can-i-solve-this-error-from-tiff-file

ModuleNotFoundError: No module named 'imagecodecs'

有人知道这个问题的解决办法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-07-14 00:23:25

我在google中也有一个类似的错误。在读取映像的" for“循环之前,我尝试了以下操作: pip安装imagecodecs

一开始不起作用,我也犯了同样的错误。然后我重新启动代码,并成功地加载了图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65111418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档