Skip to content

Commit ca79bbe

Browse files
committed
Improve VGG example with cmake.
1 parent 20cc949 commit ca79bbe

3 files changed

Lines changed: 67 additions & 52 deletions

File tree

CMakeLists.txt

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")
2424
project(MyProject LANGUAGES CXX C CHPL)
2525
message(STATUS "Using chpl: ${CMAKE_CHPL_COMPILER}")
2626

27-
set(CMAKE_C_COMPILER "/usr/bin/clang")
28-
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
27+
28+
if(APPLE)
29+
set(CMAKE_C_COMPILER "/usr/bin/clang")
30+
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
31+
endif()
2932
set(CMAKE_CXX_STANDARD 17)
3033

3134

@@ -273,43 +276,44 @@ add_dependencies(vgg ChAI)
273276
target_link_options(vgg
274277
PRIVATE
275278
# -M ${PROJECT_ROOT_DIR}/examples/vgg
279+
-svggExampleDir="${PROJECT_ROOT_DIR}/examples/vgg"
276280
${CHAI_LINKER_ARGS}
277281
)
278282

279283

280284

281-
add_custom_command(
282-
TARGET vgg
283-
POST_BUILD
284-
COMMAND ${CMAKE_COMMAND} -E copy_directory
285-
"${PROJECT_ROOT_DIR}/examples/vgg/imagenet"
286-
"$<TARGET_FILE_DIR:vgg>/vgg_example/imagenet"
287-
COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/imagenet to $<TARGET_FILE_DIR:vgg>/vgg_example/imagenet"
288-
)
289-
290-
add_custom_command(
291-
TARGET vgg
292-
POST_BUILD
293-
COMMAND ${CMAKE_COMMAND} -E copy_directory
294-
"${PROJECT_ROOT_DIR}/examples/vgg/models"
295-
"$<TARGET_FILE_DIR:vgg>/vgg_example/models"
296-
COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/models to $<TARGET_FILE_DIR:vgg>/vgg_example/models"
297-
)
298-
299-
add_custom_command(
300-
TARGET vgg
301-
POST_BUILD
302-
COMMAND ${CMAKE_COMMAND} -E copy_directory
303-
"${PROJECT_ROOT_DIR}/examples/vgg/imgs"
304-
"$<TARGET_FILE_DIR:vgg>/vgg_example/images"
305-
COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/imgs to $<TARGET_FILE_DIR:vgg>/vgg_example/images"
306-
)
307-
308-
add_custom_command(
309-
TARGET vgg
310-
POST_BUILD
311-
COMMAND ${CMAKE_COMMAND} -E rename
312-
"$<TARGET_FILE_DIR:vgg>/vgg"
313-
"$<TARGET_FILE_DIR:vgg>/vgg_example/vgg"
314-
COMMENT "Moving executable $<TARGET_FILE_DIR:vgg>/vgg to $<TARGET_FILE_DIR:vgg>/vgg_example/vgg"
315-
)
285+
# add_custom_command(
286+
# TARGET vgg
287+
# POST_BUILD
288+
# COMMAND ${CMAKE_COMMAND} -E copy_directory
289+
# "${PROJECT_ROOT_DIR}/examples/vgg/imagenet"
290+
# "$<TARGET_FILE_DIR:vgg>/vgg_example/imagenet"
291+
# COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/imagenet to $<TARGET_FILE_DIR:vgg>/vgg_example/imagenet"
292+
# )
293+
294+
# add_custom_command(
295+
# TARGET vgg
296+
# POST_BUILD
297+
# COMMAND ${CMAKE_COMMAND} -E copy_directory
298+
# "${PROJECT_ROOT_DIR}/examples/vgg/models"
299+
# "$<TARGET_FILE_DIR:vgg>/vgg_example/models"
300+
# COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/models to $<TARGET_FILE_DIR:vgg>/vgg_example/models"
301+
# )
302+
303+
# add_custom_command(
304+
# TARGET vgg
305+
# POST_BUILD
306+
# COMMAND ${CMAKE_COMMAND} -E copy_directory
307+
# "${PROJECT_ROOT_DIR}/examples/vgg/imgs"
308+
# "$<TARGET_FILE_DIR:vgg>/vgg_example/images"
309+
# COMMENT "Copying ${PROJECT_ROOT_DIR}/examples/vgg/imgs to $<TARGET_FILE_DIR:vgg>/vgg_example/images"
310+
# )
311+
312+
# add_custom_command(
313+
# TARGET vgg
314+
# POST_BUILD
315+
# COMMAND ${CMAKE_COMMAND} -E rename
316+
# "$<TARGET_FILE_DIR:vgg>/vgg"
317+
# "$<TARGET_FILE_DIR:vgg>/vgg_example/vgg"
318+
# COMMENT "Moving executable $<TARGET_FILE_DIR:vgg>/vgg to $<TARGET_FILE_DIR:vgg>/vgg_example/vgg"
319+
# )

examples/vgg/test.chpl

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use VGG;
22
use Tensor;
33

4+
config param vggExampleDir = ".";
5+
6+
writeln("VGG Example Directory: ", vggExampleDir);
7+
48
config const k = 5;
5-
config const labelFile = "imagenet/LOC_synset_mapping.txt";
9+
config const modelDir = vggExampleDir + "/models/vgg16/";
10+
config const labelFile = vggExampleDir + "/imagenet/LOC_synset_mapping.txt";
611

712

813
proc getLabels(): [] {
@@ -24,22 +29,28 @@ proc confidence(x: []): [] {
2429
}
2530

2631
// returns (top k indicies, top k condiences)
27-
proc run(model: borrowed, file: string) {
28-
const img = Tensor.load(file):real(32);
32+
proc run(model: shared VGG16(real(32)), file: string) {
2933

30-
writeln("Loaded image: ", file);
31-
writeln("Image shape: ", img.shape());
3234

33-
var output = model(img);
35+
writeln("Loading image: ", file);
36+
// const image: dynamicTensor(real(32)) = dynamicTensor.loadImage(imagePath=file,eltType=real(32));
37+
const imageData: ndarray(3,real(32)) = ndarray.loadImage(imagePath=file,eltType=real(32));
38+
writeln("Loaded image: ", file);
39+
writeln("Image shape: ", imageData.shape);
40+
const image: dynamicTensor(real(32)) = imageData.toTensor(); // new dynamicTensor(imageData);
41+
writeln("Converted image to dynamicTensor (or Tensor).");
3442

35-
writeln("Output shape: ", output.shape());
43+
writeln("Running model on image.");
44+
var output: dynamicTensor(real(32)) = model(image);
45+
writeln("Output shape: ", output.shape());
46+
writeln("Output type: ", output.type:string);
3647

37-
const top = output.topk(k);
38-
var topArr = top.tensorize(1).array.data;
39-
var percent = confidence(output.tensorize(1).array.data);
48+
const top = output.topk(k);
49+
var topArr = top.forceRank(1).array.data;
50+
var percent = confidence(output.forceRank(1).array.data);
4051

41-
var percentTopk = [i in 0..<k] percent(topArr[i]);
42-
return (topArr, percentTopk);
52+
var percentTopk = [i in 0..<k] percent(topArr[i]);
53+
return (topArr, percentTopk);
4354
}
4455

4556
proc main(args: [] string) {
@@ -48,11 +59,11 @@ proc main(args: [] string) {
4859
writeln("Loaded ", labels.size, " labels.");
4960

5061
writeln("Constructing VGG16 model.");
51-
const vgg = new VGG16(real(32));
62+
const vgg = new shared VGG16(real(32));
5263
writeln("Constructed VGG16 model.");
5364

5465
writeln("Loading VGG16 model weights.");
55-
vgg.loadPyTorchDump("models/vgg16/", false);
66+
vgg.loadPyTorchDump(modelDir, false);
5667
writeln("Loaded VGG16 model.");
5768

5869

lib/NDArray.chpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ proc type ndarray.random(shape: ?rank*int,type eltType = defaultEltType,seed: in
22022202
return ndarray.randomArray((...shape),eltType,new Random.randomStream(eltType,seed));
22032203

22042204

2205-
proc type ndarray.loadImage(imagePath: string, type eltType = defaultEltType): ndarray(3,eltType) {
2205+
proc type ndarray.loadImage(imagePath: string, type eltType = defaultEltType): ndarray(3,eltType) throws {
22062206
import Image;
22072207

22082208
param chanBits = Image.bitsPerColor;

0 commit comments

Comments
 (0)