gpt4 book ai didi

python - 将我的图像矢量化、 reshape 和标准化为 mnist.train.images 等图像

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:04 28 4
gpt4 key购买 nike

如何对图像进行矢量化、 reshape 和标准化,使其与 mnist.train.images 中的其中一张图像的大小矢量相同?到目前为止我已经尝试过以下方法但没有成功:

import os
import re
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
import os,sys
#import Image
from PIL import Image
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

# Load data and check the shape of the first mnist.train.images image
data_dir = 'temp'
mnist = read_data_sets(data_dir)
print("tmnist.train.images[0].shape is")
print(mnist.train.images[0].shape) # the result is (784,)

def resize_image(image):
img = Image.open(image)
arr = np.array(img)
#my mind is drawing a blank... additional code to make this work...
return arr


resize_image("test.png")

最佳答案

以下应该有效:

def resize_image(image):
img = Image.open(image)
img = img.resize((28, 28))
arr = np.array(img)

#convert to gray scale
if len(arr.shape) > 2:
arr = np.mean(arr, 2)

#flatten
arr = arr.flatten()
return arr

关于python - 将我的图像矢量化、 reshape 和标准化为 mnist.train.images 等图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270298/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com