代码之家  ›  专栏  ›  技术社区  ›  Rishik Mani

无法在本地驱动器上存储张量图像

  •  0
  • Rishik Mani  · 技术社区  · 6 年前

    这是我第一次尝试使用tensorflow进行任何操作。我有一个1920*1080的图像,我想要一个随机的512*512补丁。裁剪完成后,我无法将图像存储在本地驱动器上。

    import tensorflow as tf
    
    # An image of format 1920*1080
    filename = tf.train.string_input_producer(["/home/rishik/Downloads/Image.jpg"])
    
    reader = tf.WholeFileReader()
    key, value = reader.read(filename)
    
    # use png or jpg decoder based on your files.
    my_img = tf.image.decode_jpeg(value, channels=3)
    
    random_patch = tf.random_crop(my_img, [512,512,3])
    
    enc = tf.image.encode_jpeg(random_patch)
    cropped_file = tf.write_file("/home/rishik/Downloads/Image_2.jpg", enc)
    
    sess = tf.Session()
    result = sess.run(cropped_file)
    

    程序执行后,终端显示以下消息,不执行其他操作:

    "2018-07-02 00:50:09.414478: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA".
    

    有人能让我明白我在这里做错了什么吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Rishik Mani    6 年前

    在进行一些更改后,我成功地将图像保存到本地驱动器。尽管如此,警告信息仍然会出现(我认为这是我的设置的问题)。请在下面找到解决方案。

    import tensorflow as tf
    
    filepath = "<Some filepath>"
    filename = "<Some image filename>"
    tensors = []
    
    # decode image to RGB format
    image_decoded = tf.image.decode_jpeg(tf.read_file(filepath + filename), channels=3)
    
    # crop a random patch from the image of size 512*512
    random_patch = tf.random_crop(image_decoded, [512,512,3]) 
    
    # append the tensor to the list of rest of the tensors
    tensors.append(random_patch)
    
    # encode the random patch into JPEG format and then write the file to the local drive
    enc = tf.image.encode_jpeg(random_patch)
    
    # write the new cropped file at the directory random_patches created
    image = filepath + "random_patches/" + filename
    cropped_file = tf.constant(image)
    fwrite = tf.write_file(cropped_file, enc) 
    
    sess = tf.Session()
    result = sess.run(fwrite)