Search code examples
pythonscipypython-imageio

How to convert from scipy.misc.imresize to imageio


Hi I'm running a slightly expensive aws... And trying to solve old scipy.imread to the new imagio.read standard.

In this file https://github.com/ml5js/training-styletransfer/blob/master/src/utils.py

    import scipy.misc, numpy as np, os, sys

def save_img(out_path, img):
    img = np.clip(img, 0, 255).astype(np.uint8)
    scipy.misc.imsave(out_path, img)

def scale_img(style_path, style_scale):
    scale = float(style_scale)
    o0, o1, o2 = scipy.misc.imread(style_path, mode='RGB').shape
    scale = float(style_scale)
    new_shape = (int(o0 * scale), int(o1 * scale), o2)
    style_target = _get_img(style_path, img_size=new_shape)
    return style_target

def get_img(src, img_size=False):
   img = scipy.misc.imread(src, mode='RGB') # misc.imresize(, (256, 256, 3))
   if not (len(img.shape) == 3 and img.shape[2] == 3):
       img = np.dstack((img,img,img))
   if img_size != False:
       img = scipy.misc.imresize(img, img_size)
   return img

def exists(p, msg):
    assert os.path.exists(p), msg

def list_files(in_path):
    files = []
    for (dirpath, dirnames, filenames) in os.walk(in_path):
        files.extend(filenames)
        break

    return files

There is imresize how do I translate this into the new standard.

(there are several more errors showing up like mode should be pilmode?)

Secundary question is there an easy way to convert these files?

There where a lot of issues running the code of the project.

But this is one I can't seem to fix

WARNING:tensorflow:From /home/ubuntu/mlquinten/lib/python3.7/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
ml5.js Style Transfer Training!
Note: This traning will take a couple of hours.
Training is starting!...
Train set has been trimmed slightly..
(1, 708, 500, 3)
UID: 97
Traceback (most recent call last):
  File "style.py", line 179, in <module>
    main()
  File "style.py", line 156, in main
    for preds, losses, i, epoch in optimize(*args, **kwargs):
  File "src/optimize.py", line 107, in optimize
    X_batch[j] = get_img(img_p, (256,256,3)).astype(np.float32)
  File "src/utils.py", line 22, in get_img
    img = scipy.misc.imresize(img, img_size)
AttributeError: module 'scipy.misc' has no attribute 'imresize'

Solution

  • imresize is not part of scipy anymore. You can either downgrade to scipy i.e. 1.2.1 or install scikit-image and call skimage.transform.resize instead