Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

data-prep.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  1. import cv2
  2. import os
  3. import numpy as np
  4. import keras
  5. import tensorflow as tf
  6. #import shutil
  7. from keras.utils.np_utils import to_categorical
  8. path_prefix = 'classification/'
  9. class_list = os.listdir(path_prefix)
  10. num_classes = len(class_list)
  11. image_size = (224,224)
  12. def pre_npy_data(setname):
  13. print('preparing',setname,'data....')
  14. data = []
  15. label = []
  16. for i in range(len(class_list)):
  17. # print(class_list[i])
  18. if class_list[i]=='Missing':
  19. class_label = 1
  20. elif class_list[i]=='No_missing':
  21. class_label = 0
  22. img_list = os.listdir(path_prefix+class_list[i]+'/'+setname+'/')
  23. for j in range(len(img_list)):
  24. img_name = path_prefix+class_list[i]+'/'+setname+'/'+img_list[j]
  25. img = cv2.imread(img_name)
  26. # print(img_name)
  27. img = cv2.resize(img,image_size)
  28. data.append(img)
  29. label.append(class_label)
  30. data = np.array(data,'float32')
  31. label = np.array(label)
  32. shuffle_id = np.arange(len(data))
  33. np.random.shuffle(shuffle_id)
  34. data = data[shuffle_id, :,:,:]
  35. label = label[shuffle_id]
  36. cat_label = keras.utils.to_categorical(label, num_classes)
  37. np.save('data/'+setname+'_data.npy', data)
  38. np.save('data/'+setname+'_label.npy', cat_label)
  39. if __name__ == '__main__':
  40. pre_npy_data('train')
  41. pre_npy_data('val')
  42. pre_npy_data('test')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...