Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

python - Issues saving the autoencoder predicted image to a new directory

I trained an autoencoder model in Keras to generate denoised images given noisy images. The predicted images are stored in the "result" directory, with the individual filenames appended with the "denoised" tag. When I run the code, nothing is stored in the "result" folder. When I ran for an individual image, the final cv2.imwrite throws this error: "TypeError: Expected Ptr<cv::UMat> for argument 'img'". Is there a different way to write the predicted files to the destination folder without using openCV in this case?

import glob
from keras.preprocessing import image

test = glob.glob("data/test1/*.png") #all test data that needs to be denoised
test.sort()
result = "data/result" #the folder that contains predicted denoised images
#load model
model = load_model('autoencoder_model.h5')
model.summary()
#compile the model
model.compile(loss='mean_squared_error', optimizer = RMSprop())

for f in test:
    img = image.load_img(f,grayscale=True,target_size=(128,128),interpolation="nearest")
    img_name = f.split(os.sep)[-1]
    #preprocess the image
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x /= 255 
    #predict on the image
    preds = model.predict(x)
    pred_norm = 255 * (preds - preds.min()) / (preds.max() - preds.min())
    pred_normed = np.array(pred_norm, np.int)    
    cv2.imwrite(os.path.join(result,f[:-4]+'_removed.png'), pred_normed)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...