-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup_fresh_model_v4.js
190 lines (177 loc) · 7.68 KB
/
setup_fresh_model_v4.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
const apiKey = process.env.CLARIFAI_API_KEY.trim().replace(/[^a-zA-Z0-9]/g, '');
metadata.set('authorization', `Key ${apiKey}`);
async function setupFreshModel() {
try {
const modelId = 'catsdogstest';
// Step 1: Delete existing model if it exists
console.log('Step 1: Deleting existing model...');
try {
await new Promise((resolve, reject) => {
stub.DeleteModel(
{ model_id: modelId },
metadata,
(err, response) => {
if (err) {
console.log('No existing model to delete:', err);
resolve();
} else {
console.log('Model deletion response:', JSON.stringify(response, null, 2));
resolve();
}
}
);
});
} catch (error) {
console.log('Deletion error (continuing):', error);
}
// Wait for deletion to process
console.log('Waiting for deletion to process...');
await new Promise(resolve => setTimeout(resolve, 2000));
// Step 2: Create new model with recommended template
console.log('Step 2: Creating new model...');
const createResponse = await new Promise((resolve, reject) => {
const modelRequest = {
models: [{
id: modelId,
name: 'Cats and Dogs Classifier',
model_type_id: 'visual-classifier',
notes: 'Binary classifier for cats and dogs using MMClassification_ResNet_50_RSB_A1 template',
output_info: {
data: {
concepts: [
{ id: 'cats', name: 'cats' },
{ id: 'dogs', name: 'dogs' }
]
},
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true
},
params: {
template: 'MMClassification_ResNet_50_RSB_A1',
pretrained_weights: 'ImageNet-1k',
image_size: 224,
batch_size: 32,
num_epochs: 10,
flip_probability: 0.5,
flip_direction: 'horizontal'
}
},
train_info: {
params: {
template: 'MMClassification_ResNet_50_RSB_A1',
pretrained_weights: 'ImageNet-1k',
image_size: 224,
batch_size: 32,
num_epochs: 10,
flip_probability: 0.5,
flip_direction: 'horizontal',
concepts_mutually_exclusive: true
}
}
}]
};
console.log('Model request:', JSON.stringify(modelRequest, null, 2));
stub.PostModels(
modelRequest,
metadata,
(err, response) => {
if (err) {
console.error('Error creating model:', err);
reject(err);
} else {
console.log('Model creation response:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
// Wait for creation to process
console.log('Waiting for creation to process...');
await new Promise(resolve => setTimeout(resolve, 2000));
// Step 3: Verify model configuration
console.log('Step 3: Verifying model configuration...');
const verifyResponse = await new Promise((resolve, reject) => {
stub.GetModel(
{ model_id: modelId },
metadata,
(err, response) => {
if (err) {
console.error('Error verifying model:', err);
reject(err);
} else {
console.log('Model structure:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
if (verifyResponse.model.output_info) {
console.log('Success: Model created with proper configuration');
// Step 4: Create initial version
console.log('Step 4: Creating initial version...');
const versionResponse = await new Promise((resolve, reject) => {
stub.PostModelVersions(
{
model_id: modelId,
version: {
output_info: {
data: {
concepts: [
{ id: 'cats', name: 'cats' },
{ id: 'dogs', name: 'dogs' }
]
},
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true
},
params: {
template: 'MMClassification_ResNet_50_RSB_A1',
pretrained_weights: 'ImageNet-1k',
image_size: 224,
batch_size: 32,
num_epochs: 10,
flip_probability: 0.5,
flip_direction: 'horizontal'
}
},
train_info: {
params: {
template: 'MMClassification_ResNet_50_RSB_A1',
pretrained_weights: 'ImageNet-1k',
image_size: 224,
batch_size: 32,
num_epochs: 10,
flip_probability: 0.5,
flip_direction: 'horizontal',
concepts_mutually_exclusive: true
}
}
}
},
metadata,
(err, response) => {
if (err) {
console.error('Error creating version:', err);
reject(err);
} else {
console.log('Version creation response:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
console.log('Model setup completed successfully');
} else {
throw new Error('Model creation failed - output_info is not configured');
}
} catch (error) {
console.error('Setup failed:', error);
process.exit(1);
}
}
setupFreshModel();