{"cells":[{"metadata":{"_uuid":"866143ad90a09ed82fbfbd5af06303d6b8cecb4e"},"cell_type":"markdown","source":"This snippet is a multithreaded drop-in replacement for the prepareImages() method used in several kernels. Runs a lot quicker on my threadripper. I compard the results and they match perfectly."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os\n\nfrom keras.preprocessing import image\nfrom keras.applications.imagenet_utils import preprocess_input\n#from keras.applications.vgg19 import preprocess_input\n#from keras.applications.mobilenet import preprocess_input\n\n# time measuring\nimport time","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb998d99f3f7cf963034998b51ffb46796d621f8"},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f632801bc33a16becf3de2d097114f70b7e61a78"},"cell_type":"code","source":"from joblib import Parallel, delayed\nimport multiprocessing\n\ndef preprocess_image(index, data, dataset):\n    fig = data.iloc[index]['Image']\n    #load images into images of size 100x100x3\n    img = image.load_img(\"../input/\"+dataset+\"/\"+fig, target_size=(100, 100, 3))\n    x = image.img_to_array(img)\n    x = preprocess_input(x)\n    \n    return x\n    \n\ndef prepareImages_parallel(data, m, dataset):\n    print(\"Preparing images\")\n    X_train = np.zeros((m, 100, 100, 3))  \n    X_train = Parallel(n_jobs=-1, prefer=\"threads\") (delayed(preprocess_image) \n                                        (i, data, dataset) for i in range(len(data)))\n    \n    return X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aee3f76c0689534df96f8727389cf873520a4e0a"},"cell_type":"code","source":"start = time.time()\nX = prepareImages_parallel(train_df, train_df.shape[0], \"train\")\nX = np.array(X, dtype='float64')\nX /= 255\nprint('multithreaded:', time.time() - start)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba11f19a0f3d925ffcd1d855cfd00720df82be6f"},"cell_type":"code","source":"def prepareImages(data, m, dataset):\n    print(\"Preparing images\")\n    X_train = np.zeros((m, 100, 100, 3))\n    count = 0\n    \n    for fig in data['Image']:\n        #load images into images of size 100x100x3\n        img = image.load_img(\"../input/\"+dataset+\"/\"+fig, target_size=(100, 100, 3))\n        x = image.img_to_array(img)\n        x = preprocess_input(x)\n\n        X_train[count] = x\n        if (count%500 == 0):\n            print(\"Processing image: \", count+1, \", \", fig)\n        count += 1\n    \n    return X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dff481795ce7d62be9472e9c4b14069cf58f50c2"},"cell_type":"code","source":"start = time.time()\nX = prepareImages(train_df, train_df.shape[0], \"train\")\nX /= 255\nprint('single-threaded:', time.time() - start)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}