|
16 | 16 | from keras.datasets import cifar10
|
17 | 17 | from ModelConstructor import ModelConstructor
|
18 | 18 | from tensorflow.keras.utils import to_categorical
|
19 |
| -from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model |
20 | 19 | from keras.preprocessing.image import ImageDataGenerator
|
| 20 | +import tensorflow as tf |
21 | 21 | import argparse
|
22 | 22 |
|
23 | 23 | if __name__ == "__main__":
|
|
50 | 50 |
|
51 | 51 | print("\n>>> Constructing Model...")
|
52 | 52 | constructor = ModelConstructor(arch, nn_config)
|
53 |
| - test_model = constructor.build_model() |
54 |
| - print(">>> Model Constructed Successfully\n") |
55 | 53 |
|
56 |
| - if num_gpus > 1: |
57 |
| - test_model = multi_gpu_model(test_model, gpus=num_gpus) |
| 54 | + physical_gpus_num = len(tf.config.experimental.list_physical_devices('GPU')) |
| 55 | + devices = [] |
| 56 | + if 1 <= num_gpus <= physical_gpus_num: |
| 57 | + devices = ["/gpu:"+str(i) for i in range(physical_gpus_num)] |
| 58 | + else: |
| 59 | + physical_cpu_num = len(tf.config.experimental.list_physical_devices('CPU')) |
| 60 | + devices = ["/cpu:"+str(j) for j in range(physical_cpu_num)] |
| 61 | + |
| 62 | + strategy = tf.distribute.MirroredStrategy(devices=devices) |
| 63 | + with strategy.scope(): |
| 64 | + test_model = constructor.build_model() |
| 65 | + test_model.summary() |
| 66 | + test_model.compile(loss=keras.losses.categorical_crossentropy, |
| 67 | + optimizer=keras.optimizers.Adam(learning_rate=1e-3, decay=1e-4), |
| 68 | + metrics=['accuracy']) |
58 | 69 |
|
59 |
| - test_model.summary() |
60 |
| - test_model.compile(loss=keras.losses.categorical_crossentropy, |
61 |
| - optimizer=keras.optimizers.Adam(learning_rate=1e-3, decay=1e-4), |
62 |
| - metrics=['accuracy']) |
| 70 | + print(">>> Model Constructed Successfully\n") |
63 | 71 |
|
64 | 72 | (x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
65 | 73 | x_train = x_train.astype('float32')
|
|
0 commit comments