tensorflow圖像裁剪進行數(shù)據(jù)增強操作
我就廢話不多說了,大家還是直接看代碼吧~
#!/usr/bin/env python # encoding: utf-8 ''' @author: lele Ye @contact: 1750112338@qq.com @software: pycharm 2018.2 @file: 13mnist.py @time: 2018/12/17 10:23 @desc: ''' import tensorflow as tf import scipy.misc import matplotlib.pyplot as plt import random # 讀取圖像可任意大小 filenames = ['./tianchi.jpg'] # 創(chuàng)建文件讀取隊列 filename_queue = tf.train.string_input_producer(filenames) # 一個閱讀器,讀取整個文件,返回文件名稱key,以及文件中所有的內(nèi)容value reader = tf.WholeFileReader() # Returns the next record (key, value) pair produced by a reader key, value = reader.read(filename_queue) images = tf.image.decode_jpeg(value) # tf.image.decode_png(value) target_width = target_height = 224 # 裁切圖片 with tf.Session() as sess: # Coordinator的使用,用于多線程的協(xié)調(diào) coord = tf.train.Coordinator() # 啟動所有g(shù)raph收集到的隊列運行器(queuerunners) threads = tf.train.start_queue_runners(coord=coord) height,width,channels = sess.run(tf.shape(images)) offset_height = random.randint(0,height-target_height) offset_width = random.randint(0,width-target_width) reshapeimg = tf.image.crop_to_bounding_box(images, offset_height=offset_height, offset_width=offset_width, target_height=target_height,target_width=target_width) print(type(reshapeimg)) # <class 'tensorflow.python.framework.ops.Tensor'> reimg1 = reshapeimg.eval() # reimg1的類型是<class 'numpy.ndarray'> scipy.misc.imsave('./crop.jpg', reimg1) plt.imshow(reimg1) plt.axis("off") plt.show() # 請求線程結(jié)束 coord.request_stop() # 等待線程終止 coord.join(threads)
原始圖像480x320x3:
裁剪后224x224x3:
補充知識:Tensorflow 圖像增強(ImageDataGenerator)
當(dāng)我們訓(xùn)練一個較為復(fù)雜的網(wǎng)絡(luò),并且我們的訓(xùn)練數(shù)據(jù)集有限時,網(wǎng)絡(luò)十分容易陷入過擬合的狀態(tài)。
解決這個問題的一個可能的有效方法是:進行數(shù)據(jù)增強,即通過已有的有限的數(shù)據(jù)集,通過圖像處理等方法(旋轉(zhuǎn),剪切,縮放…),獲得更多的,類似的,多樣化的數(shù)據(jù)。
數(shù)據(jù)增強處理,不會占用更多的存儲空間,即在數(shù)據(jù)增強過程中,原始的數(shù)據(jù)不會被修改,所有的處理過程都是在內(nèi)存中 即時(on-the-fly) 的處理。
注意:
數(shù)據(jù)增強不一定是萬能藥(雖然數(shù)據(jù)多了),數(shù)據(jù)增強提高了原始數(shù)據(jù)的隨機性,但是若 測試集或應(yīng)用場景 并不具有這樣的隨機性,那么它將不會起到作用,還會增加訓(xùn)練所需的時間。
使用方法:
train_datagen = ImageDataGenerator( rescale=1./255, #數(shù)據(jù)值除以255,[0-255] ->[0,1] shear_range=0.2, #剪切強度(逆時針方向的剪切角度,以度為單位) zoom_range=0.2, #隨機縮放范圍 horizontal_flip=True) #水平翻轉(zhuǎn) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( 'data/train', target_size=(150, 150), batch_size=32, class_mode='binary') validation_generator = test_datagen.flow_from_directory( 'data/validation', target_size=(150, 150), batch_size=32, class_mode='binary') model.fit_generator( train_generator, steps_per_epoch=2000, epochs=50, validation_data=validation_generator, validation_steps=800)
以上這篇tensorflow圖像裁剪進行數(shù)據(jù)增強操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python:Scrapy框架中Item Pipeline組件使用詳解
這篇文章主要介紹了Python:Scrapy框架中Item Pipeline組件使用詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫深入對比
對于數(shù)據(jù)分析師來說,學(xué)習(xí)數(shù)據(jù)庫最重要的就是學(xué)習(xí)它們的查詢功能。這篇文章就以這個為切入點,為大家講述如何用Python操作這3個數(shù)據(jù)庫2021-10-10OpenCV模板匹配matchTemplate的實現(xiàn)
這篇文章主要介紹了OpenCV模板匹配matchTemplate的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10在tensorflow中設(shè)置保存checkpoint的最大數(shù)量實例
今天小編就為大家分享一篇在tensorflow中設(shè)置保存checkpoint的最大數(shù)量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01一個基于flask的web應(yīng)用誕生 使用模板引擎和表單插件(2)
一個基于flask的web應(yīng)用誕生第二篇,這篇文章主要介紹了如何使用jinja2模板引擎和wtf表單插件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04python 辦公自動化——基于pyqt5和openpyxl統(tǒng)計符合要求的名單
前幾天接到的一個需求,因為學(xué)校給的名單是青年大學(xué)習(xí)已學(xué)習(xí)的名單,然而要知道未學(xué)習(xí)的名單只能從所有團員中再排查一次,過程相當(dāng)麻煩。剛好我也學(xué)過一些操作辦公軟件的基礎(chǔ),再加上最近在學(xué)pyqt5,所以我決定用python寫個自動操作文件的腳本給她用用。2021-05-05