100100 "id" : " cell-3" ,
101101 "metadata" : {},
102102 "outputs" : [],
103- "source" : [
104- " # ── Dataset pipeline ──────────────────────────────────────────────────────────\n " ,
105- " AUTOTUNE = tf.data.AUTOTUNE\n " ,
106- " \n " ,
107- " def augment(image, label):\n " ,
108- " image = tf.image.random_flip_left_right(image)\n " ,
109- " image = tf.image.random_brightness(image, 40.0)\n " ,
110- " image = tf.image.random_contrast(image, 0.75, 1.25)\n " ,
111- " image = tf.image.random_saturation(image, 0.75, 1.25)\n " ,
112- " image = tf.image.random_crop(image, [int(INPUT_SIZE * 0.9), int(INPUT_SIZE * 0.9), 3])\n " ,
113- " image = tf.image.resize(image, [INPUT_SIZE, INPUT_SIZE])\n " ,
114- " image = tf.clip_by_value(image, 0, 255)\n " ,
115- " return image, label\n " ,
116- " \n " ,
117- " def make_dataset(split, augment_fn=None, shuffle=False):\n " ,
118- " ds = keras.utils.image_dataset_from_directory(\n " ,
119- " os.path.join(DATASET_DIR, split),\n " ,
120- " class_names=CLASSES,\n " ,
121- " image_size=(INPUT_SIZE, INPUT_SIZE),\n " ,
122- " batch_size=BATCH_SIZE,\n " ,
123- " label_mode='categorical',\n " ,
124- " shuffle=shuffle,\n " ,
125- " seed=42,\n " ,
126- " )\n " ,
127- " if augment_fn:\n " ,
128- " ds = ds.map(augment_fn, num_parallel_calls=AUTOTUNE)\n " ,
129- " return ds.prefetch(AUTOTUNE)\n " ,
130- " \n " ,
131- " train_ds = make_dataset('train', augment_fn=augment, shuffle=True)\n " ,
132- " val_ds = make_dataset('val')\n " ,
133- " test_ds = make_dataset('test')\n " ,
134- " print('Datasets loaded. Class order:', CLASSES)"
135- ]
103+ "source": "# ── Dataset pipeline ──────────────────────────────────────────────────────────\nAUTOTUNE = tf.data.AUTOTUNE\n\ndef augment(image, label):\n image = tf.image.random_flip_left_right(image)\n image = tf.image.random_brightness(image, 40.0)\n image = tf.image.random_contrast(image, 0.75, 1.25)\n image = tf.image.random_saturation(image, 0.75, 1.25)\n image = tf.image.random_crop(image, [int(INPUT_SIZE * 0.9), int(INPUT_SIZE * 0.9), 3])\n image = tf.image.resize(image, [INPUT_SIZE, INPUT_SIZE])\n image = tf.clip_by_value(image, 0, 255)\n return image, label\n\ndef make_dataset(split, augment_fn=None, shuffle=False):\n ds = keras.utils.image_dataset_from_directory(\n os.path.join(DATASET_DIR, split),\n class_names=CLASSES,\n image_size=(INPUT_SIZE, INPUT_SIZE),\n batch_size=None, # unbatched so augment receives (H, W, 3)\n label_mode='categorical',\n shuffle=shuffle,\n seed=42,\n )\n if augment_fn:\n ds = ds.map(augment_fn, num_parallel_calls=AUTOTUNE)\n ds = ds.batch(BATCH_SIZE)\n return ds.prefetch(AUTOTUNE)\n\ntrain_ds = make_dataset('train', augment_fn=augment, shuffle=True)\nval_ds = make_dataset('val')\ntest_ds = make_dataset('test')\nprint('Datasets loaded. Class order:', CLASSES)"
136104 },
137105 {
138106 "cell_type" : " code" ,
391359 },
392360 "nbformat" : 4 ,
393361 "nbformat_minor" : 5
394- }
362+ }
0 commit comments