任意のフォルダを選んで、そのフォルダの中にある画像をバッチ処理したいと考えています
python
1from PIL import Image 2import os 3import numpy as np 4import pickle 5 6directory = "ディレクトリ" 7画像フォルダ = os.listdir(directory) 8 9image_batch = np.empty((0, 151353), int) 10 11for it in 画像フォルダ: 12 13 if it == "Thumbs.db": 14 continue 15 16 image_value = np.array(Image.open(directory + "/" + it)) 17 image_value = image_value.reshape(1, image_value.size) 18 image_batch = np.append(image_batch, image_value, axis=0) 19 20with open ("猫画像バッチ処理.py", "wb") as 猫画像バッチ: 21 pickle.dump(image_batch, 猫画像バッチ)
os.listdirとイテレ―ションでフォルダの中にある全ての画像にアクセスして
reshapeで配列を1次元に直したうえで、image_batchにappendしていき、最後にpickleを使って変数を保存するというプログラムです
image_batchの初期化方法はこちらのサイトを参考にさせていただきました
http://qiita.com/fist0/items/d0779ff861356dafaf95
一応これで望んでいる処理が出来そうなのですが、画像フォルダの中には、
2512013の画像が2万近くあるせいか、実行してしばらくすると、メモリが圧迫されて、パソコンが動かなくなってしまいます。
↓こんな感じで分割して保存していくというのも考えてはみたのですが
python
1from PIL import Image 2import os 3import numpy as np 4import pickle 5 6directory = "ディレクトリ" 7画像フォルダ = os.listdir(directory) 8 9image_batch = np.empty((0, 151353), int) 10 11image_number = 0 12分割数 = 500 13pickle_name = 1 14 15while image_number != len(画像フォルダ) - (1): 16 17 image_name = "/" + 画像フォルダ[image_number] 18 19 if image_name == "/Thumbs.db": 20 image_number +=1 21 continue 22 23 image_value = np.array(Image.open(directory + image_name)) 24 image_value = image_value.reshape(1, image_value.size) 25 26 append = np.append 27 image_batch = append(image_batch, image_value, axis=0) 28 29 #分割をしながら保存していくプログラム 30 if image_batch.shape[0] == 分割数: 31 32 with open("猫画像バッチ処理" + str(pickle_name) +".py", "wb") as バッチ処理: 33 pickle.dump(image_batch, バッチ処理) 34 35 pickle_name += 1 36 image_batch = np.delete(image_batch, range(0, image_batch.shape[0]), axis=0) 37 38 image_number +=1 39
これだとメモリの圧迫を回避しながら保存していく事が出来たのですが、今度はpickleを手作業で足していくという作業が必要になってしまいます...
python
1import numpy as np 2import pickle 3import os 4 5with open("猫画像バッチ処理完了.py", "rb") as 猫画像バッチ1: 6 x = pickle.load(猫画像バッチ1) 7 8#手作業で名前を変えながら一つ一つappendしていかなければならない 9with open("猫画像バッチ処理6.py", "rb") as 猫画像バッチ: 10 y = pickle.load(猫画像バッチ) 11 12x = np.append(x, y, axis=0) 13 14with open("猫画像バッチ処理完了.py", "wb") as n: 15 pickle.dump(x, n)
手作業になると、ミスも起きやすいので、極力避けたいと思っています
メモリの圧迫を防げて、かつ手作業を極力減らせるバッチ処理を行う方法や、皆さんがどのようにバッチ処理をしているかなど、お聞きしたいです><
また、メモリは8GBあります。
追記
python
1from PIL import Image 2import os 3import numpy as np 4import pickle 5 6image_batch = np.empty((0, 151353), int) 7 8with open("猫画像バッチ処理.py", "wb") as バッチ処理: 9 pickle.dump(image_batch, バッチ処理) 10 11directory = "ディレクトリ" 12画像フォルダ = os.listdir(directory) 13 14image_number = 0 15分割数 = 200 16setter = np.empty((0, 151353), int) 17 18while image_number != len(画像フォルダ) - (1): 19 20 image_name = "/" + 画像フォルダ[image_number] 21 22 if image_name == "/Thumbs.db": 23 image_number +=1 24 continue 25 26 image_value = np.array(Image.open(directory + image_name)) 27 image_value = image_value.reshape(1, image_value.size) 28 29 append = np.append 30 setter = append(setter, image_value, axis=0) 31 32 if setter.shape[0] == 分割数: 33 34 with open("猫画像バッチ処理.py", "rb") as バッチ処理: 35 image_batch = pickle.load(バッチ処理) 36 37 image_batch = append(image_batch, setter, axis=0) 38 39 with open("猫画像バッチ処理.py", "wb") as バッチ処理: 40 pickle.dump(image_batch, バッチ処理) 41 42 print(image_batch.shape) 43 del image_batch 44 45 setter = np.delete(setter, range(0, setter.shape[0]), axis=0) 46 47 image_number +=1 48 49image_batch = append(image_batch, setter, axis=0) 50 51with open("猫画像バッチ処理.py", "wb") as バッチ処理: 52 pickle.dump(image_batch, バッチ処理)
新たの別の事を試したので、一応記載しておきます。
初期化されてある、image_batchを予め、pickle化して保存しておきます。
変数を保持する為にsetterを用意し、そこに画像の配列をappendしていき、指定した分割数が溜まれば、pickle化されてあったimage_batchを開いて、setterに溜まった配列と連結させ、それをpickleで上書き保存します。
そしてメモリを確保する為に、delとnp.deletaを使って、image_batchとsetterを削除します。
これで少しずつappendしながら保存できるので、うまくいくかと思ったのですが、タスクマネージャーの動きを見る限り、どうやらpickleをloadする時に大きくメモリを使ってしまうようです
メモリを圧迫してしまうのはやむおえない事なのでしょうか...
※回答に対するコードです。
コメントに書くとインデントがずれる為、此方に書かせて頂きます
回答を参考に書いたコードです
python
1from PIL import Image 2import os 3import numpy as np 4import pickle 5 6directory = "ディレクトリ" 7画像フォルダ = os.listdir(directory) 8 9image_list = [] 10 11for it in 画像フォルダ: 12 13 if it == "Thumbs.db": 14 continue 15 16 image_value = np.array(Image.open(directory + "/" + it)) 17 image_value = image_value.reshape(image_value.size) 18 append = image_list.append 19 append(image_value) 20 21image_batch = np.array(image_list) 22 23print(image_batch.shape) 24with open ("猫画像バッチ処理.py", "wb") as 猫画像バッチ: 25 pickle.dump(image_batch, 猫画像バッチ)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/08 08:39
2017/06/08 11:55
退会済みユーザー
2017/06/22 03:58
2017/06/22 05:38
退会済みユーザー
2017/06/22 06:10