3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Colaboratory環境への機械学習用データ展開

Last updated at Posted at 2019-01-02

#概要
Colaboratoryをガンガン利用して機械学習を行うため、インスタンスに学習データをすばやく展開する方法を探してみました。

#基本戦略
zip展開にramdisk(tmpfs)を利用する。

#事前準備
Google Driveを使用するので、Googleアカウントを準備し、学習データをアップロードしてください。

ここではGoogle Drive上に以下のように"data"ディレクトリを作成し、zip圧縮された複数の学習用イメージデータをアップロードしました。

/data/train_images_1.zip
/data/train_images_2.zip
/data/train_images_3.zip
/data/train_images_4.zip

#データ展開
Colaboratoryを起動し、以下を実行してください。
※tmpfsのサイズは学習データのサイズに応じ適宜変更のこと

import glob
import os
from google.colab import drive

drive.mount('/content/drive')
!mkdir -p ramdisk
!mount -t tmpfs -o size=5g /dev/shm ramdisk
my_drive = 'drive/My Drive/'
zipped_files = ['data']

for zipped_file in zipped_files:
  !mkdir -p $zipped_file
  for file in [os.path.basename(filename) for filename in glob.glob(my_drive+zipped_file+'*.zip')]:
    print('Deploying '+file+'...')
    !cp -n "$my_drive"$file ramdisk
    !unzip -u ramdisk/$file -d ramdisk  1> /dev/null
    !rm ramdisk/$zipped_file*.zip
    !mv ramdisk/$zipped_file/* $zipped_file
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?