M1 Conda Miniforge3 ์„ธํŒ…ํ•˜๊ธฐ(vscode)

๊น€๋‹น๊ทผยท2022๋…„ 1์›” 2์ผ
2

M1 Mac

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

๐Ÿ“Œ M1 Mac์„ ๊ตฌ๋งคํ•˜์…จ๋‚˜์š”?

Hoxy.. M1 ๊ตฌ๋งคํ•˜์…จ๋‚˜์š”?
Hoxy.. ๊ฐœ๋ฐœ์ž ์ด์‹ ๊ฐ€์š”?
Hoxy.. tensorflow๊ฐ€ ํ•„์š”ํ•˜์‹ ๊ฐ€์š”?
Hoxy.. ์‹œ๊ฐ„ ๋˜์‹œ๋ฉด ์ €๋ฅผ ๋”ฐ๋ผ์˜ค์„ธ์š” ๐Ÿฐ

M1 Macbook ๊ฐœ๋ฐœํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•˜๊ธฐ

M1 ๋งฅ์„ ๊ตฌ๋งคํ•˜๋ฉด ์•Œ์•„์•ผ ํ•  ๊ฒƒ์€ M1 ์ด๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค.
๊ธฐ์กด์˜ Intel ๊ธฐ๋ฐ˜์˜ ์‹œ์Šคํ…œ๊ณผ ๋‹ค๋ฅธ ๊ตฌ์กฐ์ด๊ธฐ์—
tensorflow ํ™˜๊ฒฝ๋„ ๋‹ฌ๋ผ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
(์ •ํ™•ํ•˜๊ฒŒ๋Š” ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ์ด๊ธด ํ•˜์ฃ  ๐Ÿฅฒ)

๊ธฐ์กด์˜ Window๋Š” anaconda ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ์šฐ๋ฆฌ์˜ M1 ์œ ์ €๋“ค์€ miniforge ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์™œ๋ƒ๊ณ ์š”?
๋ฌป์ง€ ๋งˆ์„ธ์š” ๋‚˜๋„ ๋ชฐ๋ผ์š”

miniforge ํŒŒ์ผ์€ ๊นƒํ—ˆ๋ธŒ์—์„œ ๋‹ค์šด๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

miniforge๋ฅผ Click! ํ•˜์‹œ๋ฉด

์•„๋ž˜์ฒ˜๋Ÿผ ํ™”๋ฉด์ด ๋‚˜์˜ค๋Š”๋ฐ

์ฃผํ™ฉ์ƒ‰ ์ค„์„ ๊ทธ์–ด๋†“์€ Apple Silicon ์ € ๋…€์„์„ ๋‹ค์šด ๋ฐ›์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Download ํด๋”๋กœ ๋‹ค์šด ๋ฐ›์•„์ง€๊ฒ ์ฃ ?

์ €๋Š” IDE(ํ†ตํ•ฉ๊ฐœ๋ฐœํ™˜๊ฒฝ)์„ VScode๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— VScode ์œ„์ฃผ๋กœ ์„ค๋ช… ๋“œ๋ฆด๊ป๋‹ˆ๋‹ค.

Tip. IDE ๊ฐ€ ๋ญ์ฃ ?
IDE๋Š” Integrated Development Environment์˜ ์•ฝ์ž๋กœ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ GUI๋กœ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ์นœ๊ตฌ ์ž…๋‹ˆ๋‹ค.

Tip. GUI ๊ฐ€ ๋ญ์ฃ ?
GUI๋Š” Graphical User Interface์˜ ์•ฝ์ž๋กœ ์ปดํ“จํ„ฐ์™€ ์‚ฌ๋žŒ์ด ์†Œํ†ตํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์นœ๊ตฌ ์ž…๋‹ˆ๋‹ค.

IDE : VScode, Jupyter Notebook
์–ธ์–ด : Python

โถ VScode

VScode๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

โท ํ„ฐ๋ฏธ๋„ ์‹คํ–‰

์ƒ๋‹จ ๋ฐ”์˜ VScode - ํ„ฐ๋ฏธ๋„ - ์ƒˆ ํ„ฐ๋ฏธ๋„ ํด๋ฆญ *๋‹จ์ถ•ํ‚ค [^โ‡ง']

โธ ๋ณต๋ถ™

ํ„ฐ๋ฏธ๋„ ์ฐฝ์— ํ•œ ์ค„์”ฉ ๋ณต๋ถ™ ํ•ด๋ณผ๊นŒ์š”?
(์—ฌ๋Ÿฌ๋ถ„์˜ ์†Œ์ค‘ํ•œ ์‹œ๊ฐ„์„ ์œ„ํ•ด โŒ˜+C, โŒ˜+V ํ•˜์„ธ์š”)

chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh

sh ~/Downloads/Miniforge3-MacOSX-arm64.sh

source ~/miniforge3/bin/activate

โน miniforge3 ์„ค์น˜

์ค‘๊ฐ„์ค‘๊ฐ„์— ๋‚˜์˜ค๋Š” ์„ค์น˜๋ฅผ ์œ„ํ•œ (y / n) ์—์„œ ๋ˆˆ์น˜๊ป 'y + return'๋ฅผ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

โบ python 3.8 ๋ฒ„์ „ ์„ค์น˜

์„ค์น˜๊ฐ€ ๋‹ค ๋๋‚˜๋ฉด ํ„ฐ๋ฏธ๋„์ฐฝ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณต๋ถ™ ํ•ฉ๋‹ˆ๋‹ค.

conda create --name test python=3.8
conda activate test

Tip.
miniforge๋ฅผ ์„ค์น˜ํ•˜๋ฉด ํŒŒ์ด์ฌ 3.9 ๋ฒ„์ „์ด ๊ธฐ๋ณธ์œผ๋กœ ์„ค์น˜๋œ๋‹ค๊ณ  ํ•˜๋„ค์š”?
์•„์ง๊นŒ์ง€ ํ…์„œํ”Œ๋กœ์šฐ๊ฐ€ python 3.9 ์—์„œ ์ง€์›์„ ํ•˜์ง€ ์•Š์•„์„œ python 3.8๋กœ ์ง€์ •ํ•ด์„œ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์งœ์ž”! ์•„๋ž˜์™€ ๊ฐ™์ด test ํ™˜๊ฒฝ์— ๋“ค์–ด์™€์žˆ๋‹ค๊ณ  ํ‘œ์‹œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

(test) unis@Uniui-MacBookPro % 

โž ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ์™„๋ฃŒ

์ž ๊ทธ๋Ÿฌ๋ฉด python 3.8 ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” test ๋ผ๋Š” ์ด๋ฆ„์˜ ๊ฐ€์ƒํ™˜๊ฒฝ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

์šฐ๋ฆฌ๊ฐ€ Main์œผ๋กœ ํ•™์Šตํ•  ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ธฐ ์ „์— ์ž˜ ์ž‘๋™๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ๋จผ์ € ํ•ด๋ณผ๊นŒ์š”

tensorflow-deps(TensorFlow ํŒจํ‚ค์ง€ ์ข…์† ํ•ญ๋ชฉ) ์„ค์น˜!

conda install -c apple tensorflow-deps

tensorflow-macos ์„ค์น˜!

python -m pip install tensorflow-macos

tensorflow-metal ์„ค์น˜!

(metal์€ Metal API๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋“œ์›จ์–ด ๊ฐ€์†์„ ์œ„ํ•ด ์„ค์น˜ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค)

python -m pip install tensorflow-metal

์œ„์—์„œ ๋งŒ๋“  ํ™˜๊ฒฝ์„ ํ™œ์„ฑํ™” :

conda activate test

๋ฒค์น˜๋งˆํฌ์šฉ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์น˜ :

pip install tensorflow_datasets

โž ๋ฒค์น˜๋งˆํฌ์šฉ ์ฝ”๋“œ ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ!

import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds

tf.enable_v2_behavior()

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

from tensorflow.python.compiler.mlcompute import mlcompute
mlcompute.set_mlc_device(device_name='gpu')


(ds_train, ds_test), ds_info = tfds.load(
    'mnist',
    split=['train', 'test'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
)

def normalize_img(image, label):
  """Normalizes images: `uint8` -> `float32`."""
  return tf.cast(image, tf.float32) / 255., label

batch_size = 128

ds_train = ds_train.map(
    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(batch_size)
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)


ds_test = ds_test.map(
    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_test = ds_test.batch(batch_size)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)


model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, kernel_size=(3, 3),
                 activation='relu'),
  tf.keras.layers.Conv2D(64, kernel_size=(3, 3),
                 activation='relu'),
  tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
#   tf.keras.layers.Dropout(0.25),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
#   tf.keras.layers.Dropout(0.5),
  tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=tf.keras.optimizers.Adam(0.001),
    metrics=['accuracy'],
)

model.fit(
    ds_train,
    epochs=12,
    validation_data=ds_test,
)

โž‘ ๋ฒค์น˜๋งˆํฌ Batch Size Test

์ €์˜ ๋งฅ๋ถ€๊ธฐ๋Š” MacBookAir M1 8GB Memory ๊ตฌ์„ฑ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ "batch_size = 128" ๋กœ ์„ธํŒ…ํ–ˆ์„๋•Œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.

Batch_size = 128

Mac Air ํ…Œ์ŠคํŠธMac Air ํ…Œ์ŠคํŠธ1Mac Air ํ…Œ์ŠคํŠธ2

Batch_size = 256

๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ๋ฅผ 2๋ฐฐ๋กœ ๋Š˜๋ ค์„œ ๋Œ๋ ค ๋ดค์Šต๋‹ˆ๋‹ค.

Mac Air ํ…Œ์ŠคํŠธ3Mac Air ํ…Œ์ŠคํŠธ4

โž’ ๋!


[์ฐธ๊ณ  ๋งํฌ]

qone ๋‹˜ Velog

๋งˆ๊ณ ์ปค ๋‹˜ Tistory

์šฐ๊ธฐ์˜ ์•Œ ๋‹˜ Tistory

profile
ํ™”์„ฑ์— ๊ฐ€๊ณ ํ”ˆ AI ๊ฐœ๋ฐœ์ž uni ์ž…๋‹ˆ๋‹ค.

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 3์›” 17์ผ

m1 pro ์—์„œ from tensorflow.python.compiler.mlcompute import mlcompute
์ž„ํฌํŠธ๊ฐ€ ์•ˆ๋˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋‚˜์š”??

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ