maxhuber commited on
Commit
0da089d
·
1 Parent(s): ccc35a1

Test training

Browse files
Files changed (4) hide show
  1. .gitignore +2 -1
  2. generate_prediction.py +10 -9
  3. notebook.ipynb +47 -3
  4. state.pt +2 -2
.gitignore CHANGED
@@ -2,4 +2,5 @@
2
  .idea
3
  data/
4
  __pycache__
5
- image.jpeg
 
 
2
  .idea
3
  data/
4
  __pycache__
5
+ image.jpeg
6
+ .ipynb_checkpoints
generate_prediction.py CHANGED
@@ -5,13 +5,14 @@ from model import build_hybrid_model
5
  from data_loaders import load_single_image, load_dataset, get_class_names
6
  from helpers import imshow
7
 
 
8
  def softmax(x):
9
- """Compute softmax values for each set of scores in x."""
10
  e_x = np.exp(x - np.max(x)) # Subtracting max for numerical stability
11
  return e_x / e_x.sum(axis=0)
12
 
13
- def generate_prediction(img_path="./image.jpeg"):
14
 
 
15
  N_QUBITS = 4
16
  PARAMETER_FILEPATH = "state.pt"
17
 
@@ -36,23 +37,23 @@ def generate_prediction(img_path="./image.jpeg"):
36
  # Load [image] from Disk
37
  X = load_single_image(img_path)
38
  imshow(X)
39
- input = torch.stack([X,X,X,X])
40
 
41
  # Generate Model Prediction
42
  model.eval()
43
 
44
  with torch.no_grad():
45
- input = input.to(device)
46
- output = model(input)
47
- _, preds = torch.max(output, 1)
48
 
49
  # Load Dataset for class_names
50
  class_names = get_class_names(load_dataset())
51
- print("OUTPUT VEC:",output[0])
52
- print("HOT INDEX:",preds[0])
53
 
54
  # Return Model Prediction
55
  probabilities = softmax(output[0].cpu().numpy())
56
  prediction = {class_name.replace("_", " "): prob for class_name, prob in zip(class_names, probabilities)}
57
 
58
- return prediction
 
5
  from data_loaders import load_single_image, load_dataset, get_class_names
6
  from helpers import imshow
7
 
8
+
9
  def softmax(x):
10
+ # Compute softmax values for each set of scores in x.
11
  e_x = np.exp(x - np.max(x)) # Subtracting max for numerical stability
12
  return e_x / e_x.sum(axis=0)
13
 
 
14
 
15
+ def generate_prediction(img_path="./image.jpeg"):
16
  N_QUBITS = 4
17
  PARAMETER_FILEPATH = "state.pt"
18
 
 
37
  # Load [image] from Disk
38
  X = load_single_image(img_path)
39
  imshow(X)
40
+ input = torch.stack([X, X, X, X])
41
 
42
  # Generate Model Prediction
43
  model.eval()
44
 
45
  with torch.no_grad():
46
+ input = input.to(device)
47
+ output = model(input)
48
+ _, preds = torch.max(output, 1)
49
 
50
  # Load Dataset for class_names
51
  class_names = get_class_names(load_dataset())
52
+ print("OUTPUT VEC:", output[0])
53
+ print("HOT INDEX:", preds[0])
54
 
55
  # Return Model Prediction
56
  probabilities = softmax(output[0].cpu().numpy())
57
  prediction = {class_name.replace("_", " "): prob for class_name, prob in zip(class_names, probabilities)}
58
 
59
+ return prediction
notebook.ipynb CHANGED
@@ -73,11 +73,55 @@
73
  "metadata": {
74
  "collapsed": false,
75
  "ExecuteTime": {
76
- "start_time": "2024-09-10T22:21:48.168855Z"
 
77
  }
78
  },
79
- "outputs": [],
80
- "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  },
82
  {
83
  "cell_type": "markdown",
 
73
  "metadata": {
74
  "collapsed": false,
75
  "ExecuteTime": {
76
+ "end_time": "2024-09-11T22:38:45.705122Z",
77
+ "start_time": "2024-09-11T22:16:02.896239Z"
78
  }
79
  },
80
+ "outputs": [
81
+ {
82
+ "name": "stderr",
83
+ "output_type": "stream",
84
+ "text": [
85
+ "C:\\Users\\MaxHu\\anaconda3\\envs\\quantum-env\\lib\\site-packages\\torchvision\\models\\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
86
+ " warnings.warn(\n",
87
+ "C:\\Users\\MaxHu\\anaconda3\\envs\\quantum-env\\lib\\site-packages\\torchvision\\models\\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.\n",
88
+ " warnings.warn(msg)\n"
89
+ ]
90
+ },
91
+ {
92
+ "name": "stdout",
93
+ "output_type": "stream",
94
+ "text": [
95
+ "Loading Model Weights from Disk...\n",
96
+ "Training started:\n",
97
+ "Data Loading Completed in 0m 0s\n"
98
+ ]
99
+ },
100
+ {
101
+ "name": "stderr",
102
+ "output_type": "stream",
103
+ "text": [
104
+ "C:\\Users\\MaxHu\\AppData\\Local\\Temp\\ipykernel_12896\\449253314.py:35: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
105
+ " model.load_state_dict(torch.load(PARAMETER_FILEPATH))\n"
106
+ ]
107
+ },
108
+ {
109
+ "name": "stdout",
110
+ "output_type": "stream",
111
+ "text": [
112
+ "Phase: train Epoch: 1/3 Loss: 0.8986 Acc: 0.5877 33\n",
113
+ "Phase: validation Epoch: 1/3 Loss: 1.9974 Acc: 0.3929 \n",
114
+ "Phase: train Epoch: 2/3 Loss: 0.8994 Acc: 0.5849 76\n",
115
+ "Phase: validation Epoch: 2/3 Loss: 1.9718 Acc: 0.3929 \n",
116
+ "Phase: train Epoch: 3/3 Loss: 0.8977 Acc: 0.5860 76\n",
117
+ "Phase: validation Epoch: 3/3 Loss: 2.0105 Acc: 0.3750 \n",
118
+ "Training Completed in 22m 34s\n",
119
+ "Best test loss: 1.9718 | Best test accuracy: 0.3929\n",
120
+ "Saving Model Weights to Disk...\n"
121
+ ]
122
+ }
123
+ ],
124
+ "execution_count": 1
125
  },
126
  {
127
  "cell_type": "markdown",
state.pt CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:32aadb75a8bb3957aa38971f22fe870900e8a7efc151178042d3fcd4107bec3f
3
- size 44785225
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e65747cc44ba516c9bc0c73af4887345fc0b032de1f0ea7c32ddc308ffa9cab
3
+ size 44785654